Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 50
Registered: ‎09-01-2009
My Device: Not Specified

Display downloaded images into listfield problem

hi

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);
            y=y+15;
            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??????

please reply.

Gunjan

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Display downloaded images into listfield problem

First thing to note is that:

 

UiApplication.getUiApplication().invokeLater

 

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.