1

I have been struggling with asynchronously loading images in ListView, because while they're loading, some of them are blinking (they're are replaced by another loaded image) and some of them are showed in wrong items.

Class for downloading images:

class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
  ImageView bmImage;

  public DownloadImageTask(ImageView bmImage) {
      this.bmImage = bmImage;
  }

  protected Bitmap doInBackground(String... urls) 
  {
      String urldisplay = urls[0];
      Bitmap mIcon11 = null;

      try 
      {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
      } 
      catch (Exception e) {}

      return mIcon11;
  }

  protected void onPostExecute(Bitmap result) {
      bmImage.setImageBitmap(result);
  }
}

Calls for each item, when I am assigning data to ListView:

ImageView image = (ImageView)view.findViewById(R.id.imageView);
TextView title = (TextView)view.findViewById(R.id.txtTitle);

new DownloadImageTask(image).execute(data.image);
title.setText(data.title);

What should I do differently?

bojo
  • 329
  • 4
  • 15

2 Answers2

0

Firstly, you should reconsider using this library:

https://github.com/nostra13/Android-Universal-Image-Loader

It will help you downloading images and displaying them in ListView.

Your problem is connected with recycling views in ListView. So your ImageView in getView() is reused, that's why positions in your list are changing. If you are using holder and have something like holder.image.setImageDrawable(...) inside if/else statement, you should move it outside condition, just above return.

Forin
  • 1,549
  • 2
  • 20
  • 44
0

In addition to what Forin has said, there are many libraries that really help out on things like this. My personal favorite at the moment is AQuery:

https://code.google.com/p/android-query/

Let's say you have an ImageView and you want to download an image and set the imageView to hold that image. Sounds sort of difficult, right? With AQuery this is done in one line:

aq.id(R.id.name_of_image_view).image("http://url-of-image.com/image.png");

aq in this case is an AQuery object declared like so:

AQuery aq = new AQuery();
Caleb_Allen
  • 995
  • 10
  • 19