Android 如何在Tabhost中维护多个ListView
我的问题: 如何分别查看每个选项卡的列表?如果按原样运行,当我选择brooklyn时,将显示来自bronx的相同列表 另一个问题。当我运行程序时,列表不会显示,直到您选择我选择的任何选项卡Android 如何在Tabhost中维护多个ListView,android,android-listview,android-tabhost,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.A
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>
每个选项卡都应该有自己的活动。请参阅教程。在每个活动中,您将分别定义其对应的视图,所以你会有单独的列表视图。如果我是你,我会在每个选项卡中放置活动,因为这样以后可能会给你更多的灵活性,而且你可能不会遇到这个重复的布朗克斯/布鲁克林问题。通常,将每个问题分到自己的帖子中是一个很好的做法。否决投票的原因是什么?我的答案是根据安卓开发者网站上的教程给出的。我仍然不知道为什么我对这个答案投了反对票。我不打算复制和粘贴我链接到的教程中的代码。