代码之家  ›  专栏  ›  技术社区  ›  LittleFunny

CursorAdapter如何在GridView中在android上工作

  •  4
  • LittleFunny  · 技术社区  · 11 年前

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

    我做错什么了吗?我以为只有当手机在屏幕上可见时,它才会呼叫。。

        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);
        }
    }
    
    3 回复  |  直到 11 年前
        1
  •  7
  •   Suau    11 年前
    @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像素,所以光标中的所有ImageView都可以同时放入GridView(因此每个视图都会调用newView和bindView)

        2
  •  1
  •   Lukas Olsen    11 年前

    我只需扩展BaseAdapter而不是Cursor Adapter,并通过回调将提取的数据传递给适配器。仍然没有为getThumbnail使用任何类型的不同线程——处理程序在主线程中执行,通常只用于更新UI。

    此外,您应该使用ViewHolders和convertView来加快栅格速度。

    我有这样的东西作为每个适配器的BaseAdapter:

    public abstract class MyBaseAdapter extends BaseAdapter {
    
    protected LayoutInflater inflater;
    protected Context context;
    
    public TikBaseAdapter(Context context) {
        this.context = context;
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    public final View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        if (convertView == null) {
            convertView = newView(type, parent);
        }
        bindView(position, type, convertView);
        return convertView;
    }
    
    /** Create a new instance of a view for the specified {@code type}. */
    public abstract View newView(int type, ViewGroup parent);
    
    /** Bind the data for the specified {@code position} to the {@code view}. */
    public abstract void bindView(int position, int type, View view);
    
    
    
    }
    

    My real Adapter会覆盖getItemViewType,然后使用开关盒来扩展正确的布局,并使用viewHolders(view.setTag())来加快滚动性能。只需在bindView方法中使用view.getTag(),然后编辑view项。

        3
  •  0
  •   Phantômaxx    10 年前

    据我所知,您需要将数据绑定到您创建的视图。这样地:

    public class ExampleCursorAdapter extends CursorAdapter {
    public ExampleCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }
    
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView summary = (TextView)view.findViewById(R.id.summary);
        summary.setText(cursor.getString(
                cursor.getColumnIndex(ExampleDB.KEY_EXAMPLE_SUMMARY)));
    }
    
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.item, parent, false);
        bindView(v, context, cursor);
        return v;
    }
    }