Search code examples
blackberryjava-melistfield

Loading images asynchronously on ListField Blackberry


How to display images fetched from url on listfield asynchronously? I am getting exception when i run the code. Here is my code. I am getting Uncaught NoClassFoundError

private Bitmap getBitmap(final String strEventCode)
{
    if(hst.containsKey(strEventCode))
        return (Bitmap) hst.get(strEventCode);
    else
    {           
        Thread t = new Thread(new Runnable() 
        {
            public void run() 
            {                       
                Bitmap bmp = HttpUtils.getBitmap(strHalfUrl+strEventCode+".jpg");
                hst.put(strEventCode, bmp);
            }
        });
        t.start();
    }
    return null; 
}

I draw image using following code using ListFieldCallBack:

class ListCallBack implements ListFieldCallback
{       
   public  void drawListRow(final ListField list, final net.rim.device.api.ui.Graphics g, final int index, final int y, final int w)
   {                  
       Event objEvent = (Event) eventData.elementAt(index);
       if(list.getSelectedIndex() == index)
       {              
           g.setColor(Color.LIGHTGRAY);
           g.fillRect(0, y, getWidth(), getHeight());            
       }
       Bitmap bmp = getBitmap(objEvent.getStrName());
       if(bmp==null)
           g.drawBitmap(0, y+5, loadingImage.getWidth(),loadingImage.getHeight(),loadingImage, 0, 0);
       else
           g.drawBitmap(0, y+5, bmp.getWidth(),bmp.getHeight(),bmp, 0, 0);

       g.setColor(Color.BLACK);
       int yPos = y + list.getRowHeight() - 1;
       g.drawLine(0, yPos, w, yPos);

       //final Bitmap b=(Bitmap)myImages.elementAt(index);
       //g.drawBitmap(0, y+5, b.getWidth(),b.getHeight(),b, 0, 0);         
   } 
   public Object get(ListField list, int index)
   { 
       return eventData.elementAt(index); 
   } 
   public int getPreferredWidth(ListField list)
   {
       return Display.getWidth();
   }
   public int indexOfList(ListField listField, String prefix, int start) 
   {
      return eventData.indexOf(prefix,start);
   }
}

Solution

  • What is HttpUtils.getBitmap()? If it is Java code written for Java-SE, then it will not work well on a BlackBerry, as BlackBerry devices only support Java-ME, which has substantially less capability than a modern Java-SE runtime.

    As for the async loading, you need to pass an event back to the UI once the fetch is complete. If you are fetching many photos at once, you will also want to add some sort of batching to that event, as sending an event for each photo can overrun the event queue on the UI app.