reiley
reiley

Reputation: 3761

Download images in async task and show them in ListView

I've a list of URLs, each corresponding to an image.

I need to display these images in a ListView.

Currently, my approach is pass this List<String> containing URLs to a CustomAdapter (extending BaseAdapater).

Each row item in the ListView will be inflated with an ImageView.

Then in the getView() method, if currentImageView == null, then I'll start an AsyncTask, which will download the bitmap for that imageList.get(position).

Then in the postExecute(), I'll set currentImageView.setBitmap(downloadedBitmap).

In this manner,from inside the adapter, I'll call AsyncTask one-by-one for each image.

If you think, there is something wrong in this approach then please let me know. Also, please suggest the correct approach.

One pitfall with approach I've found in when AsyncTask for a certain list element is in progress & user scrolls down then back up, the same async task will be called again.

How can I prevent this?

Upvotes: 0

Views: 178

Answers (2)

fhamicode
fhamicode

Reputation: 2518

Try this library

Then in getView method, simply add this code

UrlImageViewHelper.setUrlDrawable(holder.image, model.getimageURL(), R.drawable.placeholder);

parameter one is your ImageView, second parameter is Image URL, and the last is placeholder image.

Upvotes: 0

KennyC
KennyC

Reputation: 603

When your views get recycled, the currentImageView will not be null and it will keep displaying the same images. I would suggest using Universal Image Loader. It takes care of all of that for you, with TONS of extra features like caching and what not.

Upvotes: 1

Related Questions