开发者

Cursor Adapter and GalleryView on Android

All- Has anyone used a CursorAdapter with a Gallery widget? There are plenty of examples out there showing the Gallery and BaseAdapter(Array) as its data store.

My use-case is driving the Gallery from a SQLite cursor. The cursor has the ImageURL to display.

I have been using DroidFu's ImageLoader(with an ImageView) in other ListViews to async download the images.

But this doesnt seem to be working with the Gallery. It(Gallery) doesnt like the handler posting back to it.

So... Any thoughts of a Gallery and Cursor adapter pattern with AsyncDownlo开发者_StackOverflowad of URL based images?

Thanks


Yes, but i used my own implementation of image loader, very similar to DroidFu (with in-memory/file caching, threaded and none-threaded image loading). And it seem like you can't load image by threads with Gallery+Cursor Adapter setup, or else you get a very choppy scrolling, instead of one continuous smooth scrolling.

Here is a sample code, i use the same cursor adapter for list, gallery, and grid views.

public class CatalogCursorAdapter extends CursorAdapter {

private Context context = null;
private HLBitmapManager iMan;
private CatalogViewHolder holder;
private final LayoutInflater inflater;
private int layout;

public CatalogCursorAdapter(Context context, Cursor c, int layout)
{
    super(context, c, true);
    inflater = LayoutInflater.from(context);
    this.layout = layout;
    this.context = context;
    iMan = new HLBitmapManager(context.getCacheDir());
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view = inflater.inflate(layout, parent, false);
            return view;
}

@Override
public void bindView(View v, Context context, Cursor c) {

    String brandName = c.getString(c.getColumnIndex("display_name"));
    String category = c.getString(c.getColumnIndex("display_price"));
    String imgUrl = c.getString(c.getColumnIndex("img_url"));

    holder = (CatalogViewHolder) v.getTag();
    if(holder == null) {
        holder = new CatalogViewHolder(v);
        v.setTag(holder);
    }

    Bitmap image;
    switch (this.layout) {
    case R.layout.catalog_list_row:
        holder.title.setText(brandName);
        holder.sub_title.setText(category);
        iMan.fetchBitmapOnThread(imgUrl, Constants.EVENT_LISTVIEW_IMG_WIDTH, Constants.EVENT_LISTVIEW_IMG_HEIGHT, holder.icon);
        break;
    case R.layout.catalog_grid_cell:
        iMan.fetchBitmapOnThread(imgUrl, Constants.EVENT_LISTVIEW_IMG_WIDTH, Constants.EVENT_LISTVIEW_IMG_HEIGHT, holder.icon);
        break;
    case R.layout.catalog_slide_cell:
        image = iMan.fetchBitmap(imgUrl, Constants.EVENT_LISTVIEW_IMG_WIDTH, Constants.EVENT_LISTVIEW_IMG_HEIGHT);
        holder.icon.setImageBitmap(image);
        break;
    }
    holder.icon.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜