1

I have make on demo that list all contact from contact Uri in that I have made custom list view after adding of contact image my listview scrolling is very slow, following is my code.

public Bitmap getProfilepicture(Activity activity, String address)
    {
            Bitmap bitmap;
            Uri personUri = Uri
                .withAppendedPath(Phones.CONTENT_FILTER_URL, address);
            Cursor phoneCursor = activity.getContentResolver().query(personUri,
                PHONE_PROJECTION, null, null, null);
        if (phoneCursor.moveToFirst()) {

            int indexPersonId = phoneCursor.getColumnIndex(Phones.PERSON_ID);
            long personId = phoneCursor.getLong(indexPersonId);

            phoneCursor.close();

            Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
            bitmap = People.loadContactPhoto(activity, uri, R.drawable.icon,
                    null);
            return bitmap;
        }
        return null;
    }

with the help of method I getting contact photo that i use in my getView method. like

holder.imgViewLogo.setImageBitmap(getProfilepicture(activity,pos));

it's working proper but listview performance is low.

please help me out improve performance of listview.

Cœur
  • 37,241
  • 25
  • 195
  • 267
milind
  • 960
  • 4
  • 12
  • 38

3 Answers3

2

I suggest moving the image loading to a background task. From another thread, load all images to memory (or you can do some caching for only part of the list according to what is visible), once an image is ready, update the UI. The functional behavior will be that at first, some images will not be shown immediately but performance will be much better.

Take a look here

Community
  • 1
  • 1
Lior Ohana
  • 3,467
  • 4
  • 34
  • 49
  • hi, thanks for Responce i try with this but in that class download image from internet in my case i getting from contact uri. – milind Sep 02 '11 at 06:38
  • It doesn't matter if you download from the internet, loading from a disc or anything else, loading images requires I/O and you just need to avoid holding the UI thread (getView for example) while you get the contact image. – Lior Ohana Sep 02 '11 at 06:40
1

Perhaps caching the data would improve performance? Try adding this to your code:

private static HashMap<String address, Bitmap image> cache = new HashMap<String address, Bitmap image>();

public Bitmap getProfilepicture(Activity activity, String address) {
    if (cache.containskey(address) return cache.get(address);

    Bitmap bitmap;
       .
       .
       .
    bitmap = People.loadContactPhoto(activity, uri, R.drawable.icon,
                null);
    cache.put(address,bitmap);
    return bitmap;
}    

This would at least prevent the bitmap from being created every time getView() is called.

Depending on the life of this particular class, you may or may not make the cache static and/or save its state to file for later loading.

Chris Noldus
  • 2,432
  • 2
  • 20
  • 27
0

you need to load the images asyncronesly.

here is a thread which discusses about this.

Lazy load of images in ListView

and this response has source code mentioned as source links

GitHub: https://github.com/thest1/LazyList
Source: http://open-pim.com/tmp/LazyList.zip

Community
  • 1
  • 1
Samuel
  • 9,883
  • 5
  • 45
  • 57