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

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

问题描述

我有一个用鼠标适配器上的GridView,我用光标从媒体商店加载照片的问题。我意识到我的NewView的和bindView得到了完全调用。我的意思是假设我有500张照片,在NewView的也被调用的次数相同。

难道我做错什么?我以为它只会调用时,细胞是在屏幕上可见。

 公众诠释TASKA = 0;

公共GalleryCursorAdapter(上下文的背景下,光标C){
    超(背景下,C);
    // TODO自动生成构造函数存根
}

@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){
    // TODO自动生成方法存根
    INT指数= cursor.getColumnIndex(MediaStore.Images.Media._ID);
    长ID = cursor.getLong(指数);

    捆绑idBundle =新包();
    idBundle.putLong(身份证,身份证);

    消息味精=新的Message();
    msg.setData(idBundle);

    ImageHandler imgHandler =新ImageHandler(背景下,(ImageView的)观点);
    imgHandler.sendMessage(MSG);

    view.setTag(imgHandler);
    Log.w(任务的,伯爵);
}

@燮pressLint({NewApi,NewApi})
@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
    // TODO自动生成方法存根
    INT指数= cursor.getColumnIndex(MediaStore.Images.Media._ID);
    长ID = cursor.getLong(指数);

    ImageView的iView中=新ImageView的(上下文);

    捆绑idBundle =新包();
    idBundle.putLong(身份证,身份证);

    消息味精=新的Message();
    msg.setData(idBundle);

    ImageHandler imgHandler =新ImageHandler(背景下,iView中);
    imgHandler.sendMessage(MSG);

    iView.setTag(imgHandler);
    TASKA ++;
    Log.w(任务的,TASKA +伯爵);
    返回的iView;
}

静态类ImageHandler扩展处理程序{

    私人ImageView的MView的;
    私人语境mContext;

    公共ImageHandler(上下文C,ImageView的V){
        MVIEW = V;
        mContext = C;
    }

    @覆盖
    公共无效的handleMessage(信息MSG){

        捆绑idBundle = msg.getData();

        龙ID = idBundle.getLong(ID);
        位图图像= MediaStore.Images.Thumbnails.getThumbnail(
                mContext.getContentResolver(),
                ID,
                MediaStore.Images.Thumbnails.MICRO_KIND,
                新的选项());

        mView.setImageBitmap(图像);
    }
}
 

解决方案

  @覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
    ImageView的iView中=新ImageView的(上下文);
    iView.setLayoutParams(新GridView.LayoutParams(200,200));
    TASKA ++;
    Log.w(任务的,TASKA +伯爵);
    返回的iView;
}
 

请注意,我删除了所有的不应该是在NewView的(应该在bindView)替换新GridView.LayoutParams(200,200)与任何高度/宽度,你所需要的,不要用保鲜膜内容,内容是空的开始,导致为0x0像素,因此所有从你的光标贴合ImageViews到GridView的一次(因此NewView的和bindView获得要求每个视图)

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;
}

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)

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

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