Thursday, November 3, 2011

Simple cursor adapter made easy

Here is how to use the CursorAdapter in Android.

Outline:
In this example, we are gonna draw a list, using the CursorAdapter, for the cursor pointing to a table mapped with the list of my pojo "Promotion" basically containing the imageUrl, title and similar kind of info.
"ThumbnailFetchListener" and "downloadThumbnail" are interface and method respectively to download the images from server in the separate thread.
"promotion_banner" is the layout I use as the listViewItem.


 private class MyCursorAdapter extends CursorAdapter {


        LayoutInflater inflater;
        int titleColumnIndex;
        int imageUrlColumnIndex;

        public PromotionsGalleryAdapter(Context context, Cursor c,
                boolean autoRequery) {
            super(context, c, autoRequery);
            init(context, c);
        }
        private void init(Context context, Cursor c) {
            inflater = LayoutInflater.from(context);
            titleColumnIndex = c.getColumnIndexOrThrow(Promotions.TITLE);
            imageUrlColumnIndex = c.getColumnIndexOrThrow(Promotions.IMAGE_URL);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            Logger.log(TAG, "newView called");
            View v = inflater.inflate(R.layout.promotion_banner, null);
            v.setLayoutParams(new Gallery.LayoutParams(
                    FrameLayout.LayoutParams.FILL_PARENT,
                    FrameLayout.LayoutParams.FILL_PARENT));
            return v;
        }


        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            String title = cursor.getString(titleColumnIndex);
            String imageUrl = cursor.getString(imageUrlColumnIndex);
            Bitmap bitmap = null;
            if (TextUtils.isEmpty(imageUrl)) {
                view.findViewById(R.id.progress).setVisibility(View.GONE);
            } else {
                bitmap = BitmapCache.loadBitmap(imageUrl);
                if (bitmap == null) {
                    serviceHelper.downloadThumbnail(imageUrl,
                            new ThumbnailFetchListener());
                } else {
                    view.findViewById(R.id.progress).setVisibility(View.GONE);
                    view.findViewById(R.id.promotion_image_iv).setVisibility(
                            View.VISIBLE);
                }
            }
            ((ImageView) view.findViewById(R.id.promotion_image_iv))
                    .setImageBitmap(bitmap);
        }


        private class ThumbnailFetchListener implements
                ThumbnailDownloadListener {


            @Override
            public void onComplete(Bitmap bitmap, String url, String filename) {
                BitmapCache.save(url, bitmap);
                notifyDataSetChanged();
            }


            @Override
            public void onError(String url) {
                // ignore
            }        }    }