0

I have a ListView with 4 photos on each row, and a photo download manager handling the download of all those photos. I've limited the number of threads downloading my photos because I had way too much threads created on arrival on the ListView ( It used to be : 4 photos per row * 5 or 6 rows displayed => 20/25 Threads, and + if you scroll )

So the thing is the UI Thread gets busy, and when I click on my items while photos are loading, the OnItemClickEvent is sometimes skipped, and the user has to wait the end of the downloads of click like crazy.

Is there a way to avoid skipped event like that ?

Update : I'm sending broadcast each time a photo has been downloaded, and notifyDataSetChanged on my ListView adapter.

Camille R
  • 1,433
  • 2
  • 17
  • 29

1 Answers1

3

Creating a 20/25 threads for downloading images is something bad idea.

I think you have to use AsyncTask for downloading those images so your UIThread never blocked,

Also you can use Lazy loading list by Fedor LazyList.

Look at Lazy-Loading Images into a ListView on Android

SO Question Android - How do I do a lazy load of images in ListView

Painless Threading

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • Maybe my sentence ws not clear enough, I limited the number of threads, because it used to be as many threads as photos on the screen – Camille R Dec 21 '11 at 10:30
  • Just create a simple AsyncTask in its doInBackground() method download images and after one image download completes use pubishProgress() and onProgressUpdate method to update it in your UI, In this manner your UI Thread never blocked.. – user370305 Dec 21 '11 at 10:37
  • From 20+ threads I think its difficult to handle broadcast for different threads.. – user370305 Dec 21 '11 at 10:43
  • hum, no I limited the number of threads, and even with 2 threads I lose event – Camille R Dec 21 '11 at 10:43
  • And both threads are you starting from Main thread, Then what about the concurrency of thread?.. When both completes together.. – user370305 Dec 21 '11 at 10:46
  • Basically, I have a Thread managing a pool of threads with a limited number of threads inside; I use a ConcurrentHashMap to handle the photos queue and I have no issue with any thread neither concurrency issue – Camille R Dec 21 '11 at 10:50
  • 1
    Also setting the thread priority has had mixed results on Android in terms of getting better performance by setting it lower. Is somewhat to benefit to you. But I don't think it.. Look at this blog http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html – user370305 Dec 21 '11 at 10:53
  • I couldn't see the "setting the thread priority" in this link, but the article is really interesting – Camille R Dec 21 '11 at 10:58
  • For priorities look at this so question [Multithreading for download in Android](http://stackoverflow.com/questions/6964380/multithreading-for-download-in-android) – user370305 Dec 21 '11 at 11:01
  • Thanks ! I also found this link quite interesting http://foo.jasonhudgins.com/2010/05/limitations-of-asynctask.html, I'm using AsyncTask for my thread pool, which is probably wrong giving me strange side-effects. I'm gonna change this. Thanks – Camille R Dec 21 '11 at 11:09