tags:

views:

1027

answers:

4

I've been looking everywhere to see if there is a standard way of achieving this but I find a different solution everytime. Basically, I am trying to build a Custom ListView with an image and two-three lines of text besides it. In order to optimize it, I understand that the following have to be used:

  1. convertView: Basically if the view was already inflated, use it
  2. Lazy-Loading: Do not load the content of the rows until they are called for
  3. Background Downloading & Caching: Download images in their own threads and then update the row (and possible cache them?)

I could manage 1 and 2 but the third one is really confusing me. Is there a best practice for this?

Thanks

A: 

It may be a bit early to describe something as "best practice". AsyncTask or a thread monitoring a LinkedBlockingQueue are fine ways to offload something like image fetching.

You may be interested in my ThumbnailAdapter, which handles the background downloading and caching for you.

CommonsWare
As always, thanks for the simple answer. Thanks for pointing me to the ThumbnailAdapter. I am not quite familiar with your original AdapterWrapper class so I will go through that first before jumping into this code. As a matter of curiosity, if I am using AsynTask, who updates the ImageView? Does the thread get a handle to the ImageView or does it return a Bitmap which I use to update?Thanks
Legend
AsyncTask#onPostExecute() is run on the UI thread. It can be passed the results of your download (from AsyncTask#doInBackground()), and it can update the appropriate ImageView.
CommonsWare
Great! Thanks a lot for your input. I'll now implement a simple Caching mechanism. (Your Thumbnail adapter is of great help! Thanks a ton!). And by the way, I just got the AsynTask appraoch you suggested to work. I will post it as a response to this post.
Legend
+1  A: 

Thanks to Mark for his help. This is one way of doing what he suggested (just in case some else is curious):

private class DownloadImageTask extends AsyncTask<Object, Integer, Bitmap> {

      private ImageView iv;

      protected Bitmap doInBackground(Object... params) {
       try {
        iv = (ImageView) params[0];
                 URL aURL = new URL("http://URLTOIMAGE/img" + params[1] + ".png" ); 
                 URLConnection conn = aURL.openConnection(); 
                 conn.connect(); 
                 InputStream is = conn.getInputStream(); 
                 BufferedInputStream bis = new BufferedInputStream(is); 
                 Bitmap bm = BitmapFactory.decodeStream(bis); 
                 bis.close(); 
                 is.close();
                 return bm;
            } catch (IOException e) { 
                 return null;
            } 
      }

      protected void onPostExecute(Bitmap result) {
       iv.setImageBitmap((Bitmap) result);
      }
     }

And it would be used as follows:

new DownloadImageTask().execute(new Object[] {ImageViewHandle, position});

Note that this is not a working code. This was taken from a larger code base so you will have to make appropriate changes to make this work.

Legend
A: 

Hey Guys I Had a look at the github code of commonsware ThumbnailAdapter but I did not find SimpleWebImageCache I think this is an Hashmap but It was no where to be found can you please help me out with this...

y ramesh rao
Try here: http://github.com/commonsguy/cwac-cacheI just grabbed the jars from the ./lib folder.
GrkEngineer
A: 

hI iM a newbie and interested to try this thumbnailadapter. Is it possible to post a snippet on how to use this in an activity? or in a custom listview adapter? thanks.

Efren