0

I'm making an application in which there are few tabs on each tab click a url is called and an online xml is parsed. The data from this xml is displayed. The problem is that when the data is dispalyed the tabs disappear and only appear when i press the back button of the simulator. Whereas the data should appear below the tabs.

Please help

My UI

public class TabControl extends UiApplication {

public TabControl() {
    TabControlScreen screen = new TabControlScreen();
    pushScreen(screen);
}

public static void main(String[] args) {
    TabControl app = new TabControl();
    app.enterEventDispatcher();
}

private class TabControlScreen extends MainScreen implements FocusChangeListener {

    private Bitmap backgroundBitmap = Bitmap.getBitmapResource("rednavnar.png");

    private LabelField tab1;
    private LabelField tab2;
    private LabelField tab3;


    private VerticalFieldManager tabArea;
    private VerticalFieldManager tab1Manager;
    private VerticalFieldManager tab2Manager;
    private VerticalFieldManager tab3Manager;

    public TabControlScreen() {

        LabelField appTitle = new LabelField("Energy", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH | LabelField.FIELD_HCENTER);
        this.setTitle(appTitle);

        HorizontalFieldManager hManager = new HorizontalFieldManager( Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR | Manager.USE_ALL_WIDTH | Manager.TOPMOST) {

            // Override the paint method to draw the background image.
            public void paint(Graphics graphics) {
                // Draw the background image and then call paint.
                graphics.drawBitmap(0, 0, 700, 100, backgroundBitmap, 0, 0);
                super.paint(graphics);
            }
        };
        tab1 = new LabelField("Top News", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
            protected boolean navigationClick(int status,int time){
                tabArea = displayTab1();
                return true;
            }
        };
        LabelField separator = new LabelField("|", LabelField.NON_FOCUSABLE);
        tab2 = new LabelField("Power", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
            protected boolean navigationClick(int status,int time){
                tabArea = displayTab2();
                return true;
            }
        };
        LabelField separator1 = new LabelField("|", LabelField.NON_FOCUSABLE);
        tab3 = new LabelField("Renewable Energy", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
            protected boolean navigationClick(int status,int time){
                tabArea = displayTab3();
                return true;
            }
        };


        tab1.setFocusListener(this);
        tab2.setFocusListener(this);
        tab3.setFocusListener(this);

        hManager.add(tab1);
        hManager.add(separator);
        hManager.add(tab2);
        hManager.add(separator1);
        hManager.add(tab3);

        add(hManager);
        add(new SeparatorField());

        tab1Manager = new VerticalFieldManager();
        tab2Manager = new VerticalFieldManager();
        tab3Manager = new VerticalFieldManager();
        tabArea = displayTab1();
        add(tabArea);

    }

    public void focusChanged(Field field, int eventType) {
        if (tabArea != null) {
            if (eventType == FOCUS_GAINED) {
                if (field == tab1) {
                    delete(tabArea);
                    tabArea = displayTab1();
                    add(tabArea);
                } else if (field == tab2) {
                    delete(tabArea);
                    tabArea = displayTab2();
                    add(tabArea);
                } else if (field == tab3) {
                    delete(tabArea);
                    tabArea = displayTab3();
                    add(tabArea);
                }
            }
        }
    }

    public VerticalFieldManager displayTab1() {

        UiApplication.getUiApplication().pushScreen(new News());
        return tab1Manager;
    }

    public VerticalFieldManager displayTab2() {

        UiApplication.getUiApplication().pushScreen(new Power());
        return tab2Manager;
    }

    public VerticalFieldManager displayTab3() {

        UiApplication.getUiApplication().pushScreen(new Energy());
        return tab3Manager;
    }

}
}

My News Main Screen

public class News extends MainScreen {
public News() {

    String xmlUrl = "http://182.71.5.53:9090/solr/core4/select/?q=*";

    String[][] urlData = XmlFunctions.getURLFromXml(xmlUrl);
    for (int i = 0; i < urlData.length; i++) {
        final String title = urlData[0][i];
        final String id = urlData[1][i];
        //add(new LinkLabel(title, i));
        add(new RichTextField(title){
            protected boolean navigationClick(int status,int time){

                String cId = id;
                String bodyUrl = "http://192.168.1.44:9090/solr/core0/select/?q="+cId+";
                String[][] bodyData = XmlFunctions.getBodyFromXml(bodyUrl);
                for(int j=0;j<bodyData.length;j++){
                    String body = bodyData[0][j];
                    add(new RichTextField(body));
                }
                return true;
            }
        });
        add(new SeparatorField());
    }
}
}

Now i want to open this news screen below the tabs Please help

ReNa
  • 1,114
  • 6
  • 19
  • 37

3 Answers3

1

For your TabControl Class

public class TabControl extends UiApplication {

    public TabControl() {
        TabControlScreen screen = new TabControlScreen();
        pushScreen(screen);
    }

    public static void main(String[] args) {
        TabControl app = new TabControl();
        app.enterEventDispatcher();
    }

    private class TabControlScreen extends MainScreen {

        private Bitmap backgroundBitmap = Bitmap.getBitmapResource("rednavnar.png");

        private LabelField tab1;
        private LabelField tab2;
        private LabelField tab3;

        private VerticalFieldManager tabArea;

        public TabControlScreen() {

            LabelField appTitle = new LabelField("Energy", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH | LabelField.FIELD_HCENTER);
            this.setTitle(appTitle);

            HorizontalFieldManager hManager = new HorizontalFieldManager( Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR | Manager.USE_ALL_WIDTH | Manager.TOPMOST) {

                // Override the paint method to draw the background image.
                public void paint(Graphics graphics) {
                    // Draw the background image and then call paint.
                    graphics.drawBitmap(0, 0, 700, 100, backgroundBitmap, 0, 0);
                    super.paint(graphics);
                }
            };
            tab1 = new LabelField("Top News", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
                protected boolean navigationClick(int status,int time){
                    delete(tabArea);
                    tabArea = displayTab1();
                    add(tabArea);
                    return true;
                }
            };
            LabelField separator = new LabelField("|", LabelField.NON_FOCUSABLE);
            tab2 = new LabelField("Power", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
                protected boolean navigationClick(int status,int time){
                    delete(tabArea);
                    tabArea = displayTab2();
                    add(tabArea);
                    return true;
                }
            };
            LabelField separator1 = new LabelField("|", LabelField.NON_FOCUSABLE);
            tab3 = new LabelField("Renewable Energy", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
                protected boolean navigationClick(int status,int time){
                    delete(tabArea);
                    tabArea = displayTab3();
                    add(tabArea);
                    return true;
                }
            };

            hManager.add(tab1);
            hManager.add(separator);
            hManager.add(tab2);
            hManager.add(separator1);
            hManager.add(tab3);

            add(hManager);
            add(new SeparatorField());

            // USELESS CODE HAS BEEN REMOVED    

            tabArea = displayTab1();
            add(tabArea);

        }

        public VerticalFieldManager displayTab1() {
            return new News(); // RETURN THE MANAGER DIRECTLY
        }

        public VerticalFieldManager displayTab2() {
            return new Power(); // RETURN THE MANAGER DIRECTLY
        }

        public VerticalFieldManager displayTab3() {
            return new Energy(); // RETURN THE MANAGER DIRECTLY
        }
    }
 }

For your News class

public class News extends VerticalFieldManager { // CHANGING THE EXTENSION SUPER CLASS 
    public News() {
        super(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR);

        String xmlUrl = "http://182.71.5.53:9090/solr/core4/select/?q=*";

        String[][] urlData = XmlFunctions.getURLFromXml(xmlUrl);
        for (int i = 0; i < urlData.length; i++) {
            final String title = urlData[0][i];
            final String id = urlData[1][i];
            //add(new LinkLabel(title, i));
            add(new RichTextField(title){
                protected boolean navigationClick(int status,int time){

                    String cId = id;
                    String bodyUrl = "http://192.168.1.44:9090/solr/core0/select/?q="+cId+";
                    String[][] bodyData = XmlFunctions.getBodyFromXml(bodyUrl);
                    for(int j=0;j<bodyData.length;j++){
                        String body = bodyData[0][j];
                        add(new RichTextField(body));
                    }
                    return true;
                }
            });
            add(new SeparatorField());
        }
    }
}
Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
  • Ashraf: Thanks a ton. I've another question in the same application when i move through the tabs the tabs change on focus only whereas i want to change the tabs only on click. Can you help – ReNa May 20 '11 at 12:14
  • if you want the scenario as you said in your last comment, just comment the "focusChanged" method and its implementation. – Ashraf Bashir May 21 '11 at 13:35
  • Ashraf: Sorry for bugging you again and again, i removed the "**focusChanged**" method as told by you, but now the data of only first tab is displaying in all the tabs whether i click on other tabs or not. any work around for this will be helpful – ReNa May 23 '11 at 06:08
  • Ashraf: Thanks a lot for your help. Now it is working fine. Thank you again :) – ReNa May 26 '11 at 07:21
0

Yes, create a seprate mainscreen or screen objects and from your parent screen you can execute the below code to open new screen using the below code:

UIApplication.getUiApplication().pushScreen(your custom screen object);

Thanks

Hitesh Dangi
  • 1,496
  • 2
  • 10
  • 6
  • Hitesh: Thanks buddy for help, but i've tried this already and this opens a new screen – ReNa May 19 '11 at 11:40
0

You don't need to create 2 mainscreens in the scenario which you described.
Just create a VerticalFieldManager, add it to the mainscreen, then add content to it.

Here's a sample of code that can help you, apply it in your mainscreen constructor.

VerticalFieldManager VFM_Content = new VerticalFieldManager();
add(VFM_Content);
VFM_Content.add(/*put content here embedded in any field type you prefer*/);
Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
  • Ashraf : i am not able to completely understand. i'm putting in my code snippet please have a look – ReNa May 19 '11 at 11:34
  • OK, I got what you need, i'll send to you the fixed code, with some modification of the code you sent – Ashraf Bashir May 19 '11 at 16:42
  • My second answer contains modifications in your code with some comments showing the modifications, it may need minor changes based on your need. If this is what you want please move it up and mark it as answer :) – Ashraf Bashir May 19 '11 at 17:03