Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous image loader on listview [Android]

I'm having problems implementing an asynchronous image loader to the following code. I read some posts around the web about it and I think I understand the logic behind it, but I seem to fail in implementing it.

The code bellow is what I use to simply load the images in my listview.

public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
   Bitmap bm;

   public MyCustomAdapter(Context context, int textViewResourceId, List<RSSItem> list) {
      super(context, textViewResourceId, list); 
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      BitmapFactory.Options bmOptions;
      bmOptions = new BitmapFactory.Options();
      bmOptions.inSampleSize = 1;
      bm = LoadImage(myRssFeed.getList().get(position).getDescription(), bmOptions);

      View row = convertView;

      if(row == null) {
         LayoutInflater inflater = getLayoutInflater();
         row = inflater.inflate(R.layout.rsslist, parent, false); 
      }

      TextView listTitle = (TextView)row.findViewById(R.id.listtitle);
      listTitle.setText(myRssFeed.getList().get(position).getTitle());
      ImageView listDescription = (ImageView)row.findViewById(R.id.listdescription);
      listDescription.setImageBitmap(bm);
      TextView listPubdate = (TextView)row.findViewById(R.id.listpubdate);
      listPubdate.setText(myRssFeed.getList().get(position).getPubdate());

      return row;
   }
}
like image 579
thpoul Avatar asked Jul 05 '10 10:07

thpoul


2 Answers

You may use my sample code as reference Lazy load of images in ListView

like image 143
Fedor Avatar answered Sep 28 '22 06:09

Fedor


Have you looked SmartImageView? http://loopj.com/android-smart-image-view/

It's very simple library to load images asynchronously (:

some Features of this library

Drop-in replacement for ImageView Load images from a URL Load images from the phone’s contact address book Asynchronous loading of images, loading happens outside the UI thread Images are cached to memory and to disk for super fast loading SmartImage class is easily extendable to load from other sources

like image 30
alexandreferreira Avatar answered Sep 28 '22 06:09

alexandreferreira