使picturescrollfield无效

时间:2013-10-05 23:21:22

标签: blackberry

我正在从PictureScrollField加载来自服务器的图像,并希望在从服务器加载图像之前,PictureScrollField显示空白图像,当图像加载到图像数组中时,它重新绘制(重绘) PictureScrollField就像ListField

我从BlackBerry文档中读到每个字段都可以无效(也就是说,我们可以重新绘制它)但是当我在程序中使用PictureScrollField.invalidate()方法时,我收到错误:

  

从类型字段无效的方法不可见

我使用的程序列在下面

public final class GetMoreImage extends MainScreen {
    public static PictureScrollField psf;
    int size;
    int length;
    String text=null;
    EncodedImage[] encodedImage;
    VerticalFieldManager vmanger;
    private LoadImages loadImages;

    public GetMoreImage(int index) {
        super(NO_VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR);
        this.size=index;
        try {
            length=ListHome.object[size].getJSONArray("UrlArray").length();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ScrollEntry[] entries = new ScrollEntry[length];
        for (int i = 0; i < length; i++) {
            if(encodedImage != null && encodedImage.length > i && encodedImage[i] != null) {
                EncodedImage encodedImg =ListHome.sizeImage(JPEGEncodedImage.encode(Bitmap.getBitmapResource("icon.png"),80),640,380);
                Bitmap bmp=encodedImg.getBitmap();
                entries[i] = new ScrollEntry(bmp, "hello", "");
            }
            else {
                try {
                    text=ListHome.object[size].getJSONArray("UrlArray").getString(i).toString();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                EncodedImage encodedImg =ListHome.sizeImage(JPEGEncodedImage.encode(connectServerForImage(text),80),640,380);
                Bitmap bmp=encodedImg.getBitmap();
                entries[i] = new ScrollEntry(bmp, "hello", "");
            }
        }

        psf = new PictureScrollField();
        psf.setData(entries, 0);
        psf.setHighlightStyle(HighlightStyle.ILLUMINATE_WITH_SHRINK_LENS);
        add(psf);
        loadImages = new LoadImages(80, 80);
        loadImages.start();
    }

    private class LoadImages extends Thread {
        int widthL;
        int heightL;

        LoadImages(int width, int height) {
            this.widthL = width;
            this.heightL = height;
        }

        public void run() {
            encodedImage=new EncodedImage[length];
            if (ListHome.object[size] != null) {
                for (int i = 0; i < length; i++) {
                    try {
                        String text=ListHome.object[size].getJSONArray("UrlArray").getString(i).toString();
                        EncodedImage encodedImg = JPEGEncodedImage.encode(connectServerForImage(text), 80);//Get Image from Server
                        encodedImage[i] = ListHome.sizeImage(encodedImg, Display.getWidth(), Display.getHeight()-100);
                        psf.invalidate();//This Line generate error
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            } else {
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        Dialog.alert("No Data Found");
                    }
                });
            }
        }
    }
}

任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

您在此行收到编译错误的原因:

 psf.invalidate();//This Line generate error

是因为PictureScrollField#invalidate() method is protected, not public.因此,不在PictureScrollField中的代码或扩展PictureScrollField的类不能直接使用它。

但是,您 不能使用invalidate()invalidate()是一个低级方法,用于指示字段重绘。但是,PictureScrollField有一个更高级别的方法,旨在让您更改图像,并让字段(重新)绘制它们:PictureScrollField#setData()

因为该方法正在更改用户界面(UI),所以它应该在UI /主线程上运行。如果您在用于下载图像的run()方法内进行调用,则不会自动执行此操作。所以,你需要LoadImages类中的类似内容:

public void run() {
    encodedImage=new EncodedImage[length];
    if (ListHome.object[size] != null) {
        for (int i = 0; i < length; i++) {              
            try {
                String text=ListHome.object[size].getJSONArray("UrlArray").getString(i).toString();
                EncodedImage encodedImg = JPEGEncodedImage.encode(connectServerForImage(text), 80);//Get Image from Server                          
                encodedImage[i] = ListHome.sizeImage(encodedImg, Display.getWidth(), Display.getHeight()-100);

                //psf.invalidate();//This Line generate error
                entries[i] = new ScrollEntry(encodedImage[i].getBitmap(), "label", "callout");
                // we must update the scroll entries on the UI/main thread:
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                   // setting the field to index 'i' will scroll to the image
                   //   that was just downloaded
                   psf.setData(entries, i);
                });                        
            } catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    } 

为了完成这项工作,您必须将entries类中的本地GetMoreImage变量更改为成员变量

 public final class GetMoreImage extends MainScreen {
     public static PictureScrollField psf;
     private ScrollEntry[] entries;

但是,您仍然可以在屏幕的构造函数中实例化它(entries = new ScrollEntry[length];),或者只要知道正确的长度。