0

I made a gallery viewer based on this tutorial, and I'm having problems when clicking in the thumbnails. For example if I click in the 2nd thumbnail, it goes to the 3rd, if I click in the 4th, it goes to the 5th, etc.. but if I click on the 6th, it actually goes to the 6th. I presume it must have something to do with the position of the thumbnails and the recycling of the adapter, but I can't stumble into a solution.

This is my adapter:

public class ImageAdapter extends BaseAdapter {

        private Context ctx;
        int imageBackground;

        public ImageAdapter(Context c) {
            ctx = c;
            TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
            imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
            ta.recycle();
        }

        public int getCount() {
            return mImagesUrls.size();
        }

        public Object getItem(int arg0) {
            return arg0;
        }

        public long getItemId(int arg0) {
            return arg0;
        }

        // Thumbnails gallery

        public View getView(int arg0, View arg1, ViewGroup arg2) {
            ImageView iv;
            if (arg1 == null) {
                iv = new ImageView(ctx);
            } else {
                iv = (ImageView) arg1;
            }

            // iv.setImageResource(mImagesUrls.get(arg0));
            mCache.loadBitmap(mImagesUrls.get(arg0), iv);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);

            // Image size of the thumbnails
            iv.setLayoutParams(new Gallery.LayoutParams(200, 120));
            iv.setBackgroundResource(imageBackground);
            GalleryTitleTv.setText((arg0 + 1) + " van " + mImagesUrls.size());
            return iv;
        }
    }

Thanks a lot in advance!

EDIT: This is the caching I'm doing of the images for the thumbnails:

public void loadBitmap(String url, ImageView imageView) {
        Bitmap mImageHolder = BitmapFactory.decodeResource(context.getResources(), R.drawable.pic_2);
        final String imageKey = url;
        final Bitmap bitmap = get(imageKey);
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        } else {
            imageView.setImageBitmap(mImageHolder);
            BitmapWorkerTask task = new BitmapWorkerTask(imageView);
            task.execute(url);
        }
    }
noloman
  • 11,411
  • 20
  • 82
  • 129

1 Answers1

0

I presume it must have something to do with the position of the thumbnails and the recycling of the adapter, but I can't stumble into a solution.

Since gallery does not recycle views I think not.

            ImageView iv;
            if (arg1 == null) {
                iv = new ImageView(ctx);
            } else {
                iv = (ImageView) arg1;
            }

for the same reason this snipet is not useful. You can avoid the control and allocate the ImageView everytime, since arg1 will be always null. You should provide more information about you do cache your bitmap. IMHO the issue is cache or mImagesUrls related.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305