Search code examples
javablackberryblackberry-simulator

How to refresh screen when change focus in Blackberry


I'm developing on Blackberry OS4.5 and I have a question about " Refresh screen when change focus"

private void drawImageScrollView(Vector vImages) {
    // Horizontal Field
    HorizontalFieldManager imgScrollView = new HorizontalFieldManager(
            USE_ALL_WIDTH | HORIZONTAL_SCROLL);
    SimpleItemData itemData = new SimpleItemData();
    // Get list bitmap 
    for (int i = 0; i < vImages.size(); i++) {
        itemData = (SimpleItemData) vImages.elementAt(i);
        BitmapField bmf = new BitmapField(itemData.getImage(), BitmapField.ACTION_INVOKE | BitmapField.FOCUSABLE) {
            protected void drawFocus(Graphics graphics, boolean on) {
                graphics.setColor(Color.BLUE);
                graphics.drawRect(0, 0, getWidth() - 4, getHeight() - 4);
                graphics.drawRect(1, 1, getWidth() - 6, getHeight() - 6);

            }
            // When focus on image display new information
            protected void onFocus(int direction) {
                // Change focus and add processing here
                super.onFocus(direction);
            }

        };
        bmf.setPadding(2, 2, 2, 2);
        bmf.setMargin(2, 2, 2, 2);
        imgScrollView.add(bmf);
    }
    add(imgScrollView);
}

As above source code, you can see "onFocus" method. I want to get new data and refresh screen in here, but I don't know which method can refresh of current screen? I tried some method such as : doPaint(), invalidate(),... but not work @@ I don't have solution for this problem. Please help me if you can ...

Thanks you very much !!!


Solution

  • For this purpose better to use FocusChangeListner observe following sample it will help you how to refresh a field and reload that specified field(in my case i took one label )

    for example when ever i focus on image into my Imagescroll on downside it should display its number.

    Note:Befor test this example plese download images from here http://developer.android.com/resources/tutorials/views/hello-gridview.html it is easy to test and you have to change images according to your project

     public class StartUp extends UiApplication
    {
    
        public static void main(String[] args) {
            StartUp start=new StartUp();
            start.enterEventDispatcher();
        }
        public StartUp() 
        {
            UiApplication.getUiApplication().pushScreen(new RefreshSCreen());
    
        }
    }
    
        class RefreshSCreen extends MainScreen implements FocusChangeListener
        {
            HorizontalFieldManager imgScrollView;
            VerticalFieldManager vmanager;
            LabelField lable;
            BitmapField bmf[];
            private Vector vImages=null;
            public RefreshSCreen() {
                vImages=new Vector();
                for(int i=0;i<8;i++){
                    Bitmap image=Bitmap.getBitmapResource("sample_"+i+".jpg");
                    vImages.addElement(image);
                }
                 imgScrollView = new HorizontalFieldManager(
                            USE_ALL_WIDTH | HORIZONTAL_SCROLL);
    
                    // Get list bitmap 
                 bmf=new BitmapField[vImages.size()];
                    for (int i = 0; i < vImages.size(); i++) {
    
                        bmf[i] = new BitmapField((Bitmap) vImages.elementAt(i), BitmapField.ACTION_INVOKE | BitmapField.FOCUSABLE) {
                            protected void drawFocus(Graphics graphics, boolean on) {
                                graphics.setColor(Color.BLUE);
                                graphics.drawRect(0, 0, getWidth() - 4, getHeight() - 4);
                                graphics.drawRect(1, 1, getWidth() - 6, getHeight() - 6);
    
                            }
                        };
                        bmf[i].setFocusListener(this);
                        bmf[i].setPadding(2, 2, 2, 2);
                        bmf[i].setMargin(2, 2, 2, 2);
                        imgScrollView.add(bmf[i]);
                    }
                    add(imgScrollView);
                    vmanager=new VerticalFieldManager();
                    add(vmanager);
    
    
            }
    
            public void focusChanged(Field field, int eventType) {
    
                for(int i=0;i<vImages.size();i++){
                    if(field==bmf[i]){
                        if(bmf[i].isFocus()){
                            synchronized (UiApplication.getEventLock()) {
                                vmanager.deleteAll();
                                vmanager.invalidate();
                                lable=new LabelField("Hi I am reference of Image number "+i);
                                vmanager.add(lable);
                            }
                        }
                    }
                }
            }
        }