views:

140

answers:

2

I'm having problems to implement an acynchronous image loader to the following code. I read some posts arround 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;
   }
}
A: 

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
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.
triump
+1  A: 

You may use my sample code as reference http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012

Fedor
Oh that's really helpful! Thank you for this. I'll check it out right away.
triump
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.
triump
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