CursorAdapter如何在GridView中运行android

时间:2013-09-16 07:23:29

标签: android gridview android-cursoradapter mediastore

我在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 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

我只是扩展BaseAdapter而不是Cursor Adapter,并通过回调将获取的数据传递给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);



}

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

答案 2 :(得分: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;
}
}