0

I am currently using a custom adapter to display an ImageView and two TextViews per row in a ListView.

Within the overridden getView for the adapter, I have this code for the ImageView:

final ImageView img = (ImageView) view.findViewById(R.id.rowImg);

new Thread(new Runnable() {
    public void run() {
        final BitmapDrawable b = downloadAvatar(urlToDownload);
        img.post(new Runnable() {
            public void run() {
                img.setImageDrawable(b);
            }
        });
    }
}).start();

The downloadAvatar method basically just uses AndroidHttpClient and HttpGet. The above method works, but my question is how do I optimize it? Scrolling is choppy; I know it's probably calling getView() and downloading the image each and every time it enters the viewable area. Any tips?

arjs
  • 2,835
  • 4
  • 22
  • 18
  • If you have seen Google IO 2012 video.There they have addressed exactly same issue.Consider using LruCache to cache images.So you don't need to download them each time user scrolls.Link for video is https://developers.google.com/events/io/sessions/gooio2012/103/ – Vipul Jul 09 '12 at 12:35
  • Thanks, I'm still going through all the sessions I want to watch but that's now next on my list. – arjs Jul 09 '12 at 13:15

4 Answers4

1

Please see below Lazy Loading listview's source link and universal image loader example for that, it may help you.

Lazy Loading Listview

Android Universal Image Loader

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
0

In your case, you'll need to do what's called "lazy load of images" which will cache those images once downloaded. Check this out: Lazy load of images in ListView

Community
  • 1
  • 1
Carnal
  • 21,744
  • 6
  • 60
  • 75
0

I would suggest to use the ignition-library which has an component called RemoteImageView, which will load asynchronously url images, and the library will also keep a cache of those images, so they are not downloaded every time you re-create your ListView rows.

I use it, and I find it very usefull and robust.

theelfismike
  • 1,621
  • 12
  • 18
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
0

Here's an option:

Inside getView check if you have the current image downloaded. If it isn't downloaded yet - download it and store it in a directory of your choice. If it is present in that directory - just load it.

Depending on the image size and count you can also make some kind of cache to store the image objects required for the visible (and maybe the closest invisible above and below the visible) items.

stan0
  • 11,549
  • 6
  • 42
  • 59