是否有任何LazyLoader的图像黑莓加载图像中ListField? [英] Is there any LazyLoader for images to load image in ListField in BlackBerry?

查看:130
本文介绍了是否有任何LazyLoader的图像黑莓加载图像中ListField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新黑莓手机的发展。但良好的有关Android。

I am new to BlackBerry development. But good about android.

我要加载在ListField该服务器的图像。

I want to load Images coming from the server in ListField.

我有实施类似下面code,但没有得到成功的:

I have implement like below code but not getting success:

   package mypackage;


public class TempScreen extends MainScreen implements ListFieldCallback{
    Bitmap[] images=null;
    private ListField mylist;
    private static Bitmap _bitmap;
    private ImageDownloader downloader;
    int size = 0;
    String[] urls={
            "http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg",
            "http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg",
            "http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg",
            "http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg"};
    public TempScreen()
    {


        images=new Bitmap[urls.length];
        size = urls.length;
        mylist = new ListField();
        mylist.setCallback(this);
        mylist.setSize(4);
        mylist.setRowHeight(getFont().getHeight() * 3);
        add(mylist);
        Thread downloader=new Thread(new ImageDownloader());
        downloader.start();

    }


    public void drawListRow(ListField listField, Graphics graphics, int index,
            int y, int width) {

        if(images[index]==null)
         {
             //Load placeholder image
            _bitmap = Bitmap.getBitmapResource("close_btn.png");// load some bitmap
            // of your choice
            // here
         }
         else
             //Load Bitmap
            _bitmap = images[index]; 

        graphics.drawText("row details", 100, y + 30);
        //graphics.drawBitmap(0, y, _bitmap.getWidth(), _bitmap.getHeight(),_bitmap, 0, 0);
        mylist.invalidate(index);
    }

    public class ImageDownloader implements Runnable
    {
        public void run()
        { 
            for(int i=0; i<size;i++)
            { 
                 if(images[i]==null) 
                 { 
                      images[i]=connectServerForImage(urls[i].toString());//replace downloadImage method to whatever method       you have to download the bitmap from url 
                      UiApplication.getUiApplication().invokeLater(new Runnable(){
                          public void run()
                          {
                              mylist.invalidate();
                          }
                      });
                  }
             }
        }
    }
    public Object get(ListField listField, int index) {
        // TODO Auto-generated method stub
        return null;
    }
    public int getPreferredWidth(ListField listField) {
        // TODO Auto-generated method stub
        return 0;
    }
    public int indexOfList(ListField listField, String prefix, int start) {
        // TODO Auto-generated method stub
        return 0;
    }
    public static Bitmap connectServerForImage(String url) {
        HttpConnection httpConnection = null;
        DataOutputStream httpDataOutput = null;
        InputStream httpInput = null;
        int rc;
        Bitmap bitmp = null;
        try {
            // httpConnection = (HttpConnection)
            // Connector.open(url+";interface=wifi");
            httpConnection = (HttpConnection) Connector.open(url);
            rc = httpConnection.getResponseCode();
            // System.out.println("===============================");
            Dialog.alert("beore if condition");
            if (rc == HttpConnection.HTTP_OK) {

                System.out.println(" ============= IN FUNCTION. . . . .");
                httpInput = httpConnection.openInputStream();
                InputStream inp = httpInput;
                byte[] b = IOUtilities.streamToBytes(inp);
                EncodedImage hai = EncodedImage.createEncodedImage(b, 0,
                        b.length);
                bitmp = hai.getBitmap();
            } else {
                throw new IOException("HTTP response code: " + rc);
            }
        } catch (Exception ex) {
            System.out.println("URL Bitmap Error........" + ex.getMessage());
        } finally {
            try {
                if (httpInput != null)
                    httpInput.close();
                if (httpDataOutput != null)
                    httpDataOutput.close();
                if (httpConnection != null)
                    httpConnection.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return bitmp;
    }
}

不知道在哪里我错了。请可在任何budy帮助我一样。

Dont know where i am wrong. Please can any budy help me for the same.

推荐答案

您可以尝试使用此链接:
HTTP://www.$c$crholic.com/blackberry-webbitmapfield/

You can try using this link : http://www.coderholic.com/blackberry-webbitmapfield/

您必须创建命名为WebBitmapField一个独立的类在上面的链接建议。

You have to create a separate class named as WebBitmapField as suggested in above link.

如何使用这个类列表中的场图像的对象:

How to use that class in your list field image objects:


  • 对于每一个图像URL创建WebBitmapField对象

  • photoList_vector是通过填充元素的矢量
    单场

  • For every image url create WebBitmapField object
  • photoList_vector is the vector through which populate elements in list field

WebBitmapField web = new WebBitmapField("http://www.image1.png"); 

photoList_vector.addElement(web);

web = new WebBitmapField("http://www.image2.png"); 

photoList_vector.addElement(web);


现在使用这个载体,在你的清单领域的工作......

Now use this vector to work on your list field......

在上面的行,我们尽力确保,当我们同时发送多个请求获得的影像,然后每个图像对应于特定对象WebBitmapField

In the above lines we try to ensure that when we simultaneously send multiple requests to get the images then each image corresponds to a particular WebBitmapField Object.

每个对象被加入到载体中,从而它可以被添加到列表字段

Each object is then added to vector so that it can be added to the list field.

每个URL发送绑定到WebBitmapField的对象。

Each url send is tied to an object of WebBitmapField.

所以虽然请求是在一个单独的线程它被连接到其相关联的对象只发送

So though request is send in a separate thread it gets tied to its associated object only

希望它可以帮助
:)

Hope it helps :)

这篇关于是否有任何LazyLoader的图像黑莓加载图像中ListField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆