如何在Tabhost中维护多个ListView

时间:2011-09-16 20:49:24

标签: android android-listview android-tabhost

我的问题:  如何单独查看每个选项卡的列表?如果我按原样运行它,当我选择brooklyn时,显示的是来自bronx的相同列表。

另一个问题。我怎么做到这一点当我运行程序时,列表不会出现,直到您选择我选择的任何选项卡。

    package com.MTA_Transit;
    import android.app.Activity;
    import android.app.TabActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TabHost;


    public class MTA_Transit extends TabActivity  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost mTabHost = getTabHost();

        ListView bronx=(ListView)findViewById(R.id.bronx);
        ListView brooklyn=(ListView)findViewById(R.id.brooklyn);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>  (this,android.R.layout.simple_list_item_1,BRONX);
         bronx.setAdapter(adapter);

        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,BROOKLYN);
         brooklyn.setAdapter(adapter);


         //Bronx Tab
         mTabHost.addTab(mTabHost.newTabSpec("").setIndicator("Bronx",getResources().getDrawable(R.drawable.tab_one))
         .setContent(R.id.bronx)); 

         //Brooklyn Tab
         mTabHost.addTab(mTabHost.newTabSpec("").setIndicator("Brooklyn",getResources().getDrawable(R.drawable.tab_two))
                 .setContent(R.id.brooklyn)); 

         //Manhattan Tab
         mTabHost.addTab(mTabHost.newTabSpec("").setIndicator("Manhattan",getResources().getDrawable(R.drawable.tab_three))
                 .setContent(R.id.textview3));

         //Queens Tab
         mTabHost.addTab(mTabHost.newTabSpec("").setIndicator("Queens",getResources().getDrawable(R.drawable.tab_four))
                 .setContent(R.id.textview4));

         //Staten Island Tab
         mTabHost.addTab(mTabHost.newTabSpec("").setIndicator("Staten Island",getResources().getDrawable(R.drawable.tab_five))
                 .setContent(R.id.textview5));}

         static final String[] BRONX = new String[] {
              "Bx1 Grand Concourse","Bx2 Grand Concourse","Bx3 University Avenue/West 181 Street","Bx4","Bx4A","Bx5","Bx6","Bx7","Bx8","Bx9","Bx10","Bx11","Bx12","Bx13",
              "Bx15 3 Avenue/125 Street","Bx16 East 233 Street/Nereid Avenue","Bx17","Bx18","Bx19","Bx20","Bx21","Bx22","Bx23","Bx24" ,"Bx26","Bx27"      

              };

         static final String[] BROOKLYN = new String[] {
                 "Bx1 Grand Concourse"

         };
}

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@android:id/tabhost"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
     <LinearLayout
         android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
         <TabWidget
             android:id="@android:id/tabs"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content" />
         <FrameLayout
             android:id="@android:id/tabcontent"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
            <ListView android:id="@+id/bronx" android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:layout_weight="1"  
                > 
            </ListView> 
            <ListView android:id="@+id/brooklyn" android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:layout_weight="1" 
                > 
            </ListView> 
            <ListView android:id="@+id/list3" android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:layout_weight="1" 
                > 
            </ListView> 
            <ListView android:id="@+id/list4" android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:layout_weight="1" 
                > 
            </ListView> 



           <TextView 
                android:id="@+id/textview1"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent" 
                android:text="" />
             <TextView 
                android:id="@+id/textview2"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent" 
                android:text="" />
                <TextView 
                android:id="@+id/textview3"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent" 
                android:text="" />
                <TextView 
                android:id="@+id/textview4"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent" 
                android:text="" />
                <TextView 
                android:id="@+id/textview5"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent" 
                android:text="" />

         </FrameLayout>

     </LinearLayout>
 </TabHost>

1 个答案:

答案 0 :(得分:0)

每个标签都应有自己的活动。请参阅this教程。在每个活动中,您将分别定义其对应的视图,因此您将拥有单独的列表视图。