views:

456

answers:

2

I am using a custom adapter for my ListView as per the efficient adapter sample by Romain Guy.

In the getView() method of my adapter I am assigning an ImageView a jpg image stored on SD using the following code :

File f=new File(MovieThumbs.get(position));

if(f.length() > 0) {
    holder.thumb.setImageBitmap(BitmapFactory.decodeFile(MovieThumbs.get(position)));
}

When flicking through a list of some 200 items using this method the app suffers from bad stuttering as it tries dealing with the images.

Is there a more efficient solution for this?

+1  A: 

Rather than loading the images from within the list adapter on demand how about kicking off a thread from the onCreate of your activity to load images? As each image loads you can fire a callback to the activity to display the image in the list. The callback method would be something along the lines of:

void onImageDownloadComplete(int pos, BitMap bm) {
    ListView lv = getListView();
    View listItem = lv.getChildAt(pos);
    ImageView img = (ImageView)listItem.getChildAt(indexOfIcon);
    img.setImageBitmap(bm);
}
Jack Patmos
This will fail if the getView method in the adapter is using recycling of the view. It will make the wrong bitmap be set on wrong position. If there is 200 items in the list, i would use recycling for saving memory.
PHP_Jedi
A: 

Images need to be processed in background thread. Recycled views need to be taken into account. I try to address all these issues in my sample code, it works fine now, you may take a look http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012

Fedor