如何在 Tabhost 中维护多个 ListView

发布于 2024-12-05 07:08:13 字数 5394 浏览 0 评论 0原文

我的问题: 如何分别查看每个选项卡的列表?如果我按原样运行它,当我选择布鲁克林时,会显示布朗克斯的相同列表。

另一个问题。我该如何做到这一点,以便当我运行程序时,在您选择我选择的任何选项卡之前,列表不会出现。

    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>

My question:
How do i view the lists for each tab separately? If I run it as is, when i select brooklyn, the same list from bronx displayed.

Another question. How do i make it so when i run the program, the list does not appear until you select whatever tab i choose.

    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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话里做英雄 2024-12-12 07:08:14

每个选项卡应该具有自己的活动。请参阅 this 教程。在每个活动中,您将分别定义其相应的视图,因此您将具有单独的列表视图。

Each tab should have its own activity. Please see this tutorial. In each activity you will define its corresponding view separately, so you would have separate list views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文