CursorAdapter 如何在 GridView 中的 android 上工作 [英] How does CursorAdapter work on android in GridView

查看:26
本文介绍了CursorAdapter 如何在 GridView 中的 android 上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 gridview 上使用光标适配器时遇到问题,我使用光标从媒体商店加载照片.我意识到我的 newView 和 bindView 被完全调用了.我的意思是假设我有 500 张照片,newView 也会被调用相同的次数.

I have a problem with using cursor adapter on gridview which I used the cursor to load photos from the media store. I realized my newView and bindView got called completely. I mean assuming i have 500 photos, the newView also get called the same number of times.

我做错了什么吗?我以为它只会在单元格在屏幕上可见时调用..

Did I do anything wrong ? I thought it will only call when the cell was visible on the screen..

    public int taskA = 0;

public GalleryCursorAdapter(Context context, Cursor c) {
    super(context, c);
    // TODO Auto-generated constructor stub
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // TODO Auto-generated method stub
    int index = cursor.getColumnIndex(MediaStore.Images.Media._ID);
    long id = cursor.getLong(index);

    Bundle idBundle = new Bundle();
    idBundle.putLong("id", id);

    Message msg = new Message();
    msg.setData(idBundle);

    ImageHandler imgHandler = new ImageHandler(context, (ImageView) view);
    imgHandler.sendMessage(msg);

    view.setTag(imgHandler);
    Log.w("task s",  " count");
}

@SuppressLint({ "NewApi", "NewApi" })
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // TODO Auto-generated method stub
    int index = cursor.getColumnIndex(MediaStore.Images.Media._ID);
    long id = cursor.getLong(index);

    ImageView iView = new ImageView(context);

    Bundle idBundle = new Bundle();
    idBundle.putLong("id", id);

    Message msg = new Message();
    msg.setData(idBundle);

    ImageHandler imgHandler = new ImageHandler(context, iView);
    imgHandler.sendMessage(msg);

    iView.setTag(imgHandler);
    taskA++;
    Log.w("task s", taskA+ " count");
    return iView;
}

static class ImageHandler extends Handler {

    private ImageView mView;
    private Context mContext;

    public ImageHandler(Context c, ImageView v) {
        mView = v;
        mContext = c;
    }

    @Override
    public void handleMessage(Message msg) {

        Bundle idBundle = msg.getData();

        Long id = idBundle.getLong("id");
        Bitmap image = MediaStore.Images.Thumbnails.getThumbnail(
                mContext.getContentResolver(), 
                id, 
                MediaStore.Images.Thumbnails.MICRO_KIND, 
                new Options());

        mView.setImageBitmap(image);
    }
}

推荐答案

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    ImageView iView = new ImageView(context);
    iView.setLayoutParams(new GridView.LayoutParams(200, 200));
    taskA++;
    Log.w("task s", taskA+ " count");
    return iView;
}

注意,我删除了所有不应该在 newView 中的代码(它应该在 bindView 中)用你需要的任何高度/宽度替换 new GridView.LayoutParams(200, 200), 不要使用包装内容,因为您的内容一开始是空的,导致 0x0 像素,因此光标中的所有 ImageViews 都立即适合 GridView(因此每个视图都会调用 newView 和 bindView)

note, i removed all the code that isn't supposed to be in newView (it should be in bindView) replace new GridView.LayoutParams(200, 200) with whatever height/width you need, don't use wrap content as your content is empty at the beginning, resulting in 0x0 pixels, so ALL of the ImageViews from your cursor fit into the GridView at once (thus newView and bindView get called for every view)

这篇关于CursorAdapter 如何在 GridView 中的 android 上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆