0

This post is about Android. The following is what is happening:

I'm loading JSON and putting this into a ListView. Now, an item in this ListView is basically an ImageView and a TextView.

Because the ImageView's image can be up to 2MB large, I want to load the ImageView's Drawable async after loading the ListView (also done Async).

How should I do this? I already created 2 AsyncTasks, one for loading the list and another one for loading a Drawable based on URL. Both work, but I don't know how to inform the ListView that the Drawable is updated.

Currently, the Drawable shows when I scroll down and the item goes out of view. Then I scroll back and I see the image is updated. I think this is because I "set" the image in getView (ArrayAdapter) and this get's called when an item get's into view.

All data get's stored in an ArrayList of BlablaItem. Let's say BlablaItem contains "String title", "String imageUrl" and "Drawable image". Perfect would be to just update this Drawable in the class and that the ListView updates it too automatically...

So my question is how to do all this nicely, where to create my AsyncTask for the Drawable (currently in the loop to process JSON), where to create my AsyncTask (currently in the ArrayAdapter constructor) and how to signal the ListView that the Drawable has been updated.

John Smith
  • 965
  • 1
  • 9
  • 22
  • Use this instead of normal imageview https://github.com/sherifelkhatib/Android-Universal-Image-Loader-Wrapper – Sherif elKhatib Oct 02 '13 at 14:03
  • 1
    There are, literally, dozens of open source libraries that handle this. Some of the stand outs are http://github.com/lucasr/smoothie and http://github.com/nostra13/Android-Universal-Image-Loader – Paul Burke Oct 02 '13 at 14:03
  • I guess you should try this: http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview – Aman Gautam Oct 02 '13 at 14:04
  • see https://github.com/thest1/LazyList – ashu Oct 02 '13 at 14:05
  • Most probably, before you get an CPU issue, you will run out if memory. Especially with the size of images you work with. Make sure, that your images are small enough and space is released ad sion as possible. – jboi Oct 02 '13 at 14:41

1 Answers1

0

If the listadpater holds this ArrayList of blablaItems, it should be enough to call notifyDatasetChanged on the adapter, after a new drawable was added.

jpm
  • 3,300
  • 1
  • 19
  • 29
  • Yes, that's what I did now. I hate using external libraries and so I ended up with using an ArrayList, updating this one and calling notifyDatasetChanged whenever I add an item. When loading all items is complete, it will execute another asynctask which will in turn loop through all items, update the Drawable and call publishProgress in between every item so you'll see every image loading one by one quickly after each other. I simply call notifyDataSetChanged in onProgressUpdate so it will update. Also I used Bitmap.createScaledBitmap to scale the bitmap down to 100x100 to prevent OutOfMemory – John Smith Oct 02 '13 at 18:09