我正在开发一个应用程序,在我的班级中我有4个选项卡,在每个选项卡中我都有列表视图。我想将数据显示在列表视图中。我在数组中提供数据,但问题是当我的应用程序启动时,它会在单个选项卡下显示4个选项卡的数据。 bt当我在标签上逐个点击时,它会正确显示数据。
这是源代码..:
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 :(得分:0)
我向Josh道歉似乎我已经剪切并粘贴了错误的xml文件,我一直在搞乱使用地图选项卡和列表视图实现不同类型的布局这就是为什么你的教程在同一个工作区,这有点像我的办公桌.......非常凌乱
这是正确的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"
机器人:layout_width = “320像素” 机器人:layout_height = “58px” 机器人:SRC = “@绘制/ grasshe” &GT;
</LinearLayout>