2011-05-18 161 views
0

我正在制作一个应用程序,其中每个选项卡上有几个选项卡,单击一个URL被调用并且在线xml被解析。显示此xml的数据。问题是,当数据显示时,选项卡消失,只有当我按下模拟器的后退按钮时才会出现。而数据应该出现在标签下方。在黑莓主屏幕中打开一个新的主屏幕

请帮

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; 
    } 

} 
} 

新闻主屏

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()); 
    } 
} 
} 

现在我想打开的选项卡下面这个消息屏幕 请帮助

回答

1

为了您的TabControl类

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 
     } 
    } 
} 

为您的新闻类

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()); 
     } 
    } 
} 
+0

Ashraf:谢谢你。我在同一个应用程序中有另一个问题,当我移动标签时,标签仅在焦点上发生变化,而我只想在点击时更改标签。如果您想要像上次评论中所述的场景,可以帮助 – ReNa

+0

,只需评论“focusChanged”方法及其实施。 –

+0

Ashraf:对不起,我一遍又一遍地误会了你,我删除了你所告诉的“** focusChanged **”方法,但是现在只有第一个标签的数据在所有标签中显示,不管我是否点击其他标签。任何解决此问题的方法都将有所帮助 – ReNa

0

是,创建一个seprate mainscreen或画面对象,并从你的主画面,你可以执行以下代码使用下面的代码来打开新的画面:

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

感谢

+0

亚太区首席技术官Matt:感谢好友的帮助,但我已经尝试这样做,这将打开一个新的屏幕 – ReNa

0

你并不需要创建在你描述的场景中有2个主屏幕。
只需创建一个VerticalFieldManager,将其添加到主屏幕,然后向其添加内容。

下面是一个代码示例,它可以帮助您将它应用到主屏幕构造函数中。

VerticalFieldManager VFM_Content = new VerticalFieldManager(); 
add(VFM_Content); 
VFM_Content.add(/*put content here embedded in any field type you prefer*/); 
+0

阿什拉夫:我无法完全理解。我正在放入我的代码片段请看看 – ReNa

+0

好吧,我得到了你需要的东西,我会给你发送固定代码,对你发送的代码进行一些修改 –

+0

我的第二个答案包含你的代码修改评论显示修改,它可能需要根据您的需要稍作修改。如果这是你想要的,请将其移动并标记为答案:) –