2010-10-11 70 views
0

我正在開發一個應用程序,並在我的班級,我有4個選項卡,並且在每個選項卡內有列表視圖。我想要顯示數據到列表視圖。我提供數組中的數據,但問題是當我的應用程序啓動時,顯示單個選項卡下的4個選項卡的數據。 bt當我在標籤上一個一個的點擊它顯示正確的數據。在標籤下的Android列表視圖

這裏是源代碼..:

public class test extends TabActivity implements OnTabChangeListener 
{ 
private static final String LIST_TAB_TAG = "News"; 
private static final String LIST_TAB_TAG2 = "List2"; 
private static final String LIST_TAB_TAG3 = "List3"; 
private static final String LIST_TAB_TAG4 = "List4"; 
private TabHost tabHost,tabHost1,tabHost2,tabHost3; 
private ListView listView,listview2,listview3,listview4; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.clistview); 

    tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    // setup must be called if you are not inflating the tabhost from XML 
    tabHost.setup(); 
    tabHost.setOnTabChangedListener(this); 

    listView = (ListView) findViewById(R.id.list); 
    listview2 = (ListView) findViewById(R.id.list2); 
    listview3 = (ListView) findViewById(R.id.list3); 
    listview4 = (ListView) findViewById(R.id.list4); 

    // create some dummy coordinates to add to the list 
List<String> list1Strings = new ArrayList<String>(); 
     list1Strings.add(""); 
     list1Strings.add("Item 2"); 
     list1Strings.add("Item 3"); 
     list1Strings.add("Item 4"); 
     listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings)); 



    List<GeoPoint> pointsList2 = new ArrayList<GeoPoint>(); 
    pointsList2.add(new GeoPoint((int)(464464645), (int)(979646664))); 
    pointsList2.add(new GeoPoint((int)(7976566), (int)(987464313))); 
    listview2.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, pointsList2)); 

    List<GeoPoint> pointsList3 = new ArrayList<GeoPoint>(); 
    pointsList3.add(new GeoPoint((int)(32.864*1E6), (int)(-117.2353*1E6))); 
    pointsList3.add(new GeoPoint((int)(37.441*1E6), (int)(-122.1419*1E6))); 
    listview3.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, pointsList3)); 

    List<GeoPoint> pointsList4 = new ArrayList<GeoPoint>(); 
    pointsList4.add(new GeoPoint((int)(464464645), (int)(979646664))); 
    pointsList4.add(new GeoPoint((int)(7976566), (int)(987464313))); 
    listview4.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, pointsList4)); 

    tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG).setIndicator("News", getResources().getDrawable(R.drawable.newsicon)).setContent(new TabContentFactory() 
    { 
     public View createTabContent(String arg0) { 
      return listView; 
     } 
    })); 

    tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG2).setIndicator("Opinion", getResources().getDrawable(R.drawable.opinionicon)).setContent(new TabContentFactory() { 
     public View createTabContent(String arg0) { 
      return listview2; 
     } 
    })); 

    tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG3).setIndicator("Press", getResources().getDrawable(R.drawable.pressicon)).setContent(new TabContentFactory() { 
     public View createTabContent(String arg0) { 
      return listview3; 
     } 
    })); 

    tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG4).setIndicator("My Stocks", getResources().getDrawable(R.drawable.mystockicon)).setContent(new TabContentFactory() { 
     public View createTabContent(String arg0) { 
      return listview4; 
     } 
    })); 



    //HACK to get the list view to show up first, 
    //tabHost.setCurrentTab(1); 
    //tabHost.setCurrentTab(0); 
} 

@Override 
/*protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
}*/ 


public void onTabChanged(String tabId) { 
    // TODO Auto-generated method stub 

    } 

} 
+0

有機會你可以添加你的佈局XML文件? – 2010-10-11 16:38:38

+0

另外,是否有你擴展TabActivity的原因,但也解壓縮tabhost和調用setup()?通常,只有在不是TabActivity的情況下才會調用安裝程序。它看起來像你從我的博客上的地圖活動的代碼。我也有關於List活動的教程,可能會更容易遵循。 – 2010-10-11 16:42:37

+0

對不起喬希見下面; – JonniBravo 2010-10-14 13:22:19

回答

0

我道歉,約什似乎我剪切並粘貼了錯誤的XML文件,我一直在用的地圖選項卡和列表視圖implenting不同類型的佈局亂搞這就是爲什麼你教程是在同一個工作區,這是一個有點像我的辦公桌.......很凌亂

這是正確的xml:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="5dp"> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="25dp" 
     android:layout_weight="1"/> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"/> 
     <ImageView 
     android:id="@+id/widget30" 

的android:layout_width = 「320px的」 機器人:layout_height = 「58px」 機器人:SRC = 「@繪製/ grasshe」 >

</LinearLayout>