2011-12-12 23 views
0

我在我的tabwidget中有兩個選項卡,每個選項卡顯示一個列表視圖。 ay 最初我的第一個標籤顯示爲選中,但下方的列表對應第二個標籤。 一旦我點擊標籤,我得到正確的顯示。android tabhost選項卡和視圖不匹配

private static final String LIST_TAB_TAG1 = "UpcomingEvents"; 
private static final String LIST_TAB_TAG2 = "PastEvents"; 

tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG1) 
      .setIndicator(LIST_TAB_TAG1) 
      .setContent(new TabContentFactory() { 
       public View createTabContent(String arg) { 
        return listView1; 
       } 
      })); 
    tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG2) 
      .setIndicator(LIST_TAB_TAG2) 
      .setContent(new TabContentFactory() { 
       public View createTabContent(String arg) { 
        return listView2; 
       } 
      })); 
    tabHost.setCurrentTab(0); 

當此sctivity啓動時,LIST_TAB_TAG1將突出顯示,但顯示的列表是listview2。只有在活動開始時纔會出現此問題。點擊標籤後,其工作正常

請幫我解決這個問題。感謝您的時間

回答

0

我通常使用文檔中給出的代碼適用於我,請嘗試這一個。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 

    Intent intent; // Reusable Intent for each tab 
    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, UpcomingEvents.class); 
    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("UpcomingEvents").setIndicator("UpcomingEvents",getResources().getDrawable(R.drawable.homebutton1)).setContent(intent); 
    tabHost.addTab(spec); 
    // Do the same for the other tabs 
    intent = new Intent().setClass(this,PastEvents.class); 
    spec = tabHost.newTabSpec("PastEvents").setIndicator("PastEvents", 
         getResources().getDrawable(R.drawable.bank_transaction1)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(0); 
    } 
+0

這裏我有listviews要顯示每個選項卡。這就是爲什麼我使用這種方法。 – png

+0

好的,但上面也爲listView工作,你必須爲其活動中的各個列表設置setcontentView。 –

相關問題