7

In my Android App I have a listview containing 30 rows, and each row consists of several textviews of which one is spannable and sometimes contains a lot of formatted text and images.

Those images are loaded from the web asynchroneously: A placeholder is displayed until the image has been downloaded is then replaced by the image.

Unfortunately, the rows of the listview are loaded when I scroll over them. This makes the whole thing very slow. Also, those images are loaded from the web again and again, whenever I scroll over the row.

How can I turn it off, that the ListView rows are loaded when I scroll over them? They should be loaded once when I start the activity and never again.

Best regards and thanks in advance, Jan Oliver

3 Answers 3

5

When you do a lazy-loading ListView, is because you want to speed it up your app. Turn it off is not the best solution. So, what you can do is implementing a basic cache system in order to avoid downloading and setting the ImageView again and again.

The easiest way to do so is implementing a HashMap with URLs as keys and Bitmaps as values. Something like this:

Map cache = new HashMap();
// then, on your lazy loader
Bitmap image = cache.get(urlOfTheImage);
if( image == null ){
    // download and decode the image as normal,
    // then assign the decoded bitmap to
    // the 'image' variable
    cache.put(image);
}
imageView.setImageBitmap(image);

If those images will be the same always, meaning that each time you open the app the same images will be downloaded, then you better save those images in the filesystem and use them from there.

On the other hand, if the images tend to change, you could implement some interesting stuff: use SoftReferences. There's an explanation in this video. This can also be used if you are loading images from the filesystem.

Edit

With regards to your comment, I highly recommend you watching the video I posted. It's one hour long, but it really worths the effort. When using an adapter, checking if the convertView is null is just a simple way to improve performance, though there are some other techniques that will improve your app even more. Also, if you had have problems while using that trick, is because you are probably implementing it the wrong way. Remember: even if you don't re-inflate the views, you do have to set the value of each one of the children views, otherwise you will experience some problems.

3
  • Thanks for your answer! I will probably implement that. Anyways, I just stumbled over the argument convertView in the getView method of the ArrayAdapter. Checking this for null and if it's not null, returning it, solves my problem and speeds up the list extremly; unfortunately, it mixes up some rows while scrolling and shows a row more than once...
    – janoliver
    Nov 1, 2010 at 22:24
  • I added more info to my answer.
    – Cristian
    Nov 1, 2010 at 22:29
  • Allright. Thanks again for the answer. I will watch the video. The image cache works fine, though, and I also tried to use a cache for the rows. But that, somehow, doesn't really work. I will watch the video tomorrow and hopefully end up with a fast ListView. :)
    – janoliver
    Nov 1, 2010 at 22:45
1

If you can, start with an Image Array full of the "placeholder images", then download the images in to an Array firing an AsyncTask during on Create. During row view building just refer to the array. That way if it has the new image it will load it, if not it will get the placeholder.

If you have a lot of data its gonna get real slow and be a crappy expirience for the user.

0

Create a list of objects that represent each row. Create a loader as a background thread that updates the objects as it loads the data. Your list view will draw data from the objects.

(Not a good idea if you have hundreds of rows and a huge amount of data in each row - in that case, you should only load data within a few rows of the currently active row and have some sort of MRU cache).

2
  • Right now I have my own ArrayAdapter implementation, overriding getView to fill the listview with more complex rows. You are saying that I should kind of cache the rows in a List of objects and then the ListView will load them all at once?
    – janoliver
    Nov 1, 2010 at 22:15
  • No, the ListView will only load them upon demand, you can't change that. YOU need to load them. Create a background thread that just loads all data and fills your array. The ListView will simply load the data that is being populated by your background thread. The only other thing you need to keep in mind is that your background thread needs to update a view if it finishes loading while it's on the screen.
    – EboMike
    Nov 1, 2010 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.