【问题标题】:How does CursorAdapter work on android in GridViewCursorAdapter 如何在 GridView 中的 android 上工作
【发布时间】:2013-09-16 07:23:28
【问题描述】:

我在 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);
    }
}

【问题讨论】:

  • 请发布您的适配器代码。很难说没有看到它有什么问题。
  • 好的,我发布代码..对不起,我认为发布代码会打扰查看它的人。

标签: android gridview android-cursoradapter mediastore


【解决方案1】:
@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】:

我会简单地扩展 BaseAdapter 而不是 Cursor Adapter 并将获取的数据通过回调传递给 Adapter。您仍然没有为 getThumbnail 使用任何不同的线程 - 处理程序在主线程中执行,并且通常仅用于更新 UI。

您还应该使用 ViewHolders 和 convertView 来加快 Grid-Speed。

我有这样的东西作为每个适配器的 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);



}

我真正的 Adapter 会覆盖 getItemViewType,然后使用 switch-cases 来扩展正确的布局 - 并使用 viewHolders (view.setTag()) 来加快滚动性能。只需在 bindView 方法中使用 view.getTag(),然后编辑 View-Items。

【讨论】:

    【解决方案3】:

    据我了解,您需要将数据绑定到您创建的视图。像这样:

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

    【讨论】:

    • 在你的newView中不需要调用bindView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多