1

我正在从服务器加载图像,PictureScrollField并希望在从服务器加载图像之前,PictureScrollField显示一个空白图像,当图像加载到图像数组中时,它会重新绘制(重绘)PictureScrollField类似ListField.

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

来自类型 Field 的方法 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");
                    }
                });
            }
        }
    }
}

任何帮助将不胜感激。

谢谢

4

1 回答 1

1

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

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

是因为该PictureScrollField#invalidate() 方法是受保护的,而不是公开的。 因此,不在其中的代码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];),或者只要您知道正确的长度。

于 2013-10-06T04:43:34.953 回答