03-23-2010 02:34 AM
I have one listfield. in this i want to display image on the left side and data on left side.
here i have a url from which i want to download image and display. and i have a data that is not to be worried about.
EncodedImage image = EncodedImage.getEncodedImageResource("noimage.png"
graphics.drawImage(0, y, image.getWidth(), image.getHeight(), image,0, 0, 0);
/* comment */ UiApplication.getUiApplication().invokeLater(new setListImageClass(graphics,index,y,this));
graphics.drawText( Integer.toString(obj.getID()), 80, y, 0, w);
graphics.drawText(obj.getName(), 80, y, 0, w);
above code will display image on left and data on right side...what i want is once this much data is displayed i want to download image assosiated with each data and to be display.....so i have started one thread as you can see from /*comment*/ in that setListenerImageClass i am downloading image.... am done with downloading but my question is now how could i display that downloaded image instead of noimage.png??????
03-23-2010 05:32 AM
First thing to note is that:
does not start a new Thread. In fact this schedules a Runnable that will run on the Event Thread. If this process is attempting to download an image, then you will be blocking the Event Thread with your network code. Occasionally this works on the Simulator. It almost always fails on the device. So you need to start a real Thread.
The second issue is that the display code is being run each time the row in the ListField is displayed, which means you are potentially scheduling a download repeatedly for rows. I don't think you want to do that. I would suggest that it would be better to schedule the download independently of the ListField image processing, perhaps when you populate the ListField.
But don't start a separate download for every image in your ListField. Start a Thread that will process the entries in the ListField and sequentially obtain the images, one at a time. This is the most efficient way to complete this (though clearly not the fastest).
Thirdly this code converts a 'noimage' png to a Bitmap every time you display the row. This is an expensive operation. You really don't want to do that. Just convert it once, save the converted value and use that in the ListField.
Which leads on the solution to your question, how do I update the ListField with the correct image once it is downloaded?. When you populate the ListField, associate each item in the Field with an image. Initially they will all point to the noimage image. Then in your processing that downloads the image, update this association to the correct image for the row, once the correct image is downloaded. Then the next time you draw the row, the processing will get the correct image without doing anything.
Once you get to this stage, investigate the invalidate(..) method of the ListField and use it from the Background Thread that is downloading the image, to update the row that has the new image available.
That is it, that is the whole process. Hope it is clear.
But as you can see, not a small job.