2011-05-18 156 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

我的第二個答案包含你的代碼修改評論顯示修改,它可能需要根據您的需要稍作修改。如果這是你想要的,請將其移動並標記爲答案:) –