5

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;
   }
}
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
thpoul
  • 700
  • 2
  • 8
  • 18

3 Answers3

8

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

Community
  • 1
  • 1
Fedor
  • 43,261
  • 10
  • 79
  • 89
  • Your code is working really good when I run it on the compiler and does exactly what i'm asdking for. Unfortunately. I cannot implement it on my rss reader project. I would really appreciate a hand in this. Thank you in advance. – thpoul Jul 05 '10 at 19:35
  • You could use LazyAdapter as it is. Just pass array of URLs to it. And specify R.layout.rsslist in getView R.layout.item. Should be working after that. – Fedor Jul 05 '10 at 23:20
1

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

0

On solution would be to populate a class variable within your adapter, say, an ArrayList with the references all the "ImageView listDescription"

ArrayList<ImageView> allImageViews = new ArrayList<ImageView>();    
    ...

    public View getView(int position, View convertView, ViewGroup parent){
       ...
       ImageView listDescription=(ImageView)row.findViewById(R.id.listdescription);
       allImageViews.add(listDescription);
       ...
    }

    private class ImageDownLoader extends AsyncTask<ArrayList, Void, Void>{
       doInBackground(){
         for(ImageView imageView: allImageViews){
         BitmapFactory.Options bmOptions;
         bmOptions = new BitmapFactory.Options();
         bmOptions.inSampleSize = 1;
         bm = LoadImage(imageNameOrWhatever, bmOptions);
         imageView.setImageBitmap(bm);
       } 
    }

Then use an AsyncTask that goes through each ImageView, retrieves the associated Image and removes the ImageView from the ArrayList. It will download one at a time in the background while your gui will still respond.

androider
  • 333
  • 5
  • 13
  • Thank you for taking the time to respond! I have already tried that but I must be doing something wrong, or missing something and I haven't been able to implement it. – thpoul Jul 05 '10 at 18:02