4

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

3 回答 3

7
@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)

于 2013-09-23T06:46:56.280 回答
1

我会简单地扩展 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);



}

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

于 2013-09-16T12:52:32.850 回答
0

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

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;
}
}
于 2013-09-16T07:53:13.627 回答