0

The problem that I'm getting is:

When I scroll the listview BEFORE an item's (say x) imageview is loaded, then after scrolling, that item's (x) image gets loaded and displayed in some other item's (say y) imageview for short amount of time until that item (y) imageview is not loaded.

I'm using Android Universal Image Loader library. I have the following asynctask in bindView of my listview cursor adapter:

@Override
public void bindView(View view, Context context, Cursor cursor) {
...
    ImageView imageView = (ImageView) view.findViewById(R.id.icon);
    new DownloadImageAsyncTask(url, imageView).execute();
...
}

private class DownloadImageAsyncTask extends AsyncTask<Void, Void, Bitmap> {

        String image_link;
        ImageView imageView;
        final WeakReference<ImageView> viewReference;

        public DownloadImageAsyncTask(String image_link, ImageView imageView) {
            this.image_link = image_link;
            this.imageView = imageView;
            viewReference = new WeakReference<ImageView>( imageView );
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            ... // downloading bitmap here
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {

            ImageView imageView = viewReference.get();
            if( imageView != null ) {

                ImageLoader imageLoader = ImageLoader.getInstance();

                DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .cacheOnDisk(true).cacheInMemory(true)
                        .showImageOnFail(R.drawable.ic_contact_picture)
                        .displayer(new FadeInBitmapDisplayer(300)).build();

                imageLoader.displayImage(image_link, imageView, options); // new options will be used
            }    
        }
}

Why does image that should be in item x get displayed for short amount of time in item y (when scrolling before item x image is loaded) ?

Is internal recycling of listview items causing a problem?

user5155835
  • 4,392
  • 4
  • 53
  • 97
  • can you show your adapter's getView? – Jelle van Es Oct 27 '15 at 13:58
  • Do you have a big list with images ? – PedroAGSantos Oct 27 '15 at 13:58
  • @JellevanEs I'm using cursor adapter, which does not have getView – user5155835 Oct 27 '15 at 13:59
  • @PedroAGSantos I have around 50 items, with each item having an imageview – user5155835 Oct 27 '15 at 13:59
  • Your Viewholder pattern may be wrong. Or you may forgot setting empty image to your loading images. Also Use Picasso or Glide libraries. They are the best libraries for displaying images async. – Oğuzhan Döngül Oct 27 '15 at 14:06
  • @oguzhand since I'm using a cursor adapter, i've not used viewholder. Also i'm using universal image loader – user5155835 Oct 27 '15 at 14:08
  • Make the imageview a weakreference and check if is still valid before setting the image. Also take a look in [this](http://developer.android.com/training/displaying-bitmaps/index.html) tutorial from android developers. – Mikel Oct 27 '15 at 14:13
  • @Mikel I've updated question with weakreference, please let me know if i'm doing it correctly because it did not solve the problem – user5155835 Oct 27 '15 at 14:28
  • don't store the imageview, just the view reference and check if it is valid (not null) before loading the image – Mikel Oct 27 '15 at 14:40
  • @Mikel can you please post some sample code? – user5155835 Oct 27 '15 at 14:44
  • Check [this](http://stackoverflow.com/a/14080933/562840) answer, your problem might be elsewhere... The weakreference should work as you have it... – Mikel Oct 27 '15 at 14:51
  • @Mikel, I think the problem is in recycling of listview items. Do you have any idea how that can be solved? – user5155835 Oct 27 '15 at 14:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93510/discussion-between-mikel-and-user5155835). – Mikel Oct 27 '15 at 14:53
  • you can also take a look at volley lib if you want a tutorial the good thing about this is that catch system. http://cypressnorth.com/mobile-application-development/setting-android-google-volley-imageloader-networkimageview/ – PedroAGSantos Oct 27 '15 at 15:04

1 Answers1

0

The problem seemed to be with Android Universal Image Loader library. I just moved onto Picasso.

user5155835
  • 4,392
  • 4
  • 53
  • 97