03-07-2011 09:07 AM
I think you're going about this a litttle incorrectly. What you probably want to do is:
-Collect your items and start a Timer to add your downloads each to a TimerTask (if you have a lot of items you can exhaust your number of threads). You can have a couple Timers if you need them.
-Create your list and when you're painting the row wait and see if your Bitmap for that item is null. If it is don't draw, but if it isn't then do the drawing (probably the cause of your NullPointerException).
-As your downloads finish you can call invalidate() on your ListField so it can redraw with the images
03-07-2011 09:16 AM
No, you don't want your downloads in TimerTasks - their run() methods should never make blocking calls (which the downloads are all about). Refer to java.util.Timer docs for more info about that.
Read the following thread for some ideas on organizing image downloads:
IllegalArgumentException Loading Image from Server
There are quite detailed explanations near the end of the thread.
03-07-2011 09:24 AM
I had read previously from someone here in the forums that a TimerTask (for them specifically, not a general one that performs other tasks) is the way to do it so you don't create too many Threads and end up losing downloads. If you have like 30 downloads something is going to get blocked regardless, at least with this you control that it's something as non-critical as an image.
03-07-2011 09:30 AM - last edited on 03-07-2011 09:32 AM
Hmm so confusing now.
03-07-2011 09:31 AM
Well - putting downloads in TimerTask is obviously wrong, as their run() methods "should complete quickly" (quotation from the java.util.Timer Javadoc). Blocking calls by their nature do not "complete quickly". And any network activity (as well as filesystem!) is blocking.
Since, as you correctly stated, one cannot create Threads with impunity, the only proper solution becomes doing those downloads in one Thread, one-by-one. That is exactly what I've suggested in the thread referenced above.
03-07-2011 09:38 AM
I agree with you on the should complete quickly part, however the reason as far as I can tell from the docs is so other Tasks don't get thrown off schedule because of one Task taking too much time. I suppose the way I'm programming it is essentually a queue, but it behaves completely how I would expect and want. We are really implementing the same thing since one Timer == one Thread and each download would block the subsequent ones, I'm just not adhering to the guidelines as much since I know what it's doing and how it's going to respond.
03-09-2011 05:20 PM
This would appear to be a duplicate?
Original Poster, can you confirm these are the same problem and terminate one of the Threads?