Here are 4 references that I have found for lazy-loading images into a
listview in Android.
The idea is to display a placeholder image, get the actual image in the
background, update the ImageView in the list when the image is available.

I've tried to do this in the simplest way possible using an AsyncTask in an
Adapter.
The outline of that approach is below.  Is it flawed?
Is there an agreed approach to handling this common task?

Thanks,
Carmen

http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android-part-2/  Tom
van Zummeren
http://evancharlton.com/thoughts/lazy-loading-images-in-a-listview/ Evan
Charlton
http://code.google.com/p/shelves/ from Romain Guy
http://github.com/commonsguy/cwac-thumbnail from Mark Murphy


Approach using AsyncTask within an Adapter

Somewhere in the Adapter.
Add info about the URL for the image to fetch to the ImageView.
Execute an AsyncTask to get the image in the background and load the
ImageView.
AsyncTask uses the tag to fetch the image.
It uses the tag to find the correct view within the ListView.
ImageView imageViewByTag = (ImageView) myListView.findViewWithTag(tag);
ImageView is updated.

Somewhere in the Adapter:

myImageView.setTag("http://someimage.jpg";); //
            try {
            new GetImage().execute(myImageView);
            } catch(RejectedExecutionException e) { }


// Asynctask
private class GetImageInBackground extends AsyncTask<ImageView , ImageView ,
Long > {
        Drawable drawable;
        String tag;

        @Override
        protected void  onProgressUpdate  (ImageView... params){
        if (drawable!=null){
                  ImageView imageViewByTag = (ImageView)
myListView.findViewWithTag(tag);  // find the ImageView
                  if (imageViewByTag != null) {
                      imageViewByTag.setImageDrawable(drawable); //update it
                    }

          }
        }

        @Override
        protected Long doInBackground(ImageView... params) {
            tag = (String) params[0].getTag(); // get the tag from the
ImageView

            if (tag==null){
                drawable = getPlaceholder(); // get a default drawable
            }else{

                bm =getDrawablefromURL(String (tag); // a method to get
drawable from a URL
            }
            publishProgress(params[0]);
            return null;
        }

    }

-- 
Carmen
http://www.twitter.com/CarmenDelessio
http://www.talkingandroid.com
http://www.facebook.com/BFFPhoto
http://www.twitter.com/DroidDrop

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to