问题
Sorry if I can not speak good English, how have multiple Listview in one activity that extends ListActivity such as
private List<Tour> tours;
ArrayAdapter<Tour> adapter = new ArrayAdapter<Tour>(this,
android.R.layout.simple_list_item_1, tours);
setListAdapter(adapter);
this code is for one Listview in activity that Listview's id is @android:id/list but i have two or more Lisview in one activity Please guide me
回答1:
You should extends Activity instead of ListActivity in your code.
And in your activity's layout xml file you should take two different id for that two list view.
See below reference links for more details...
http://www.coderzheaven.com/2012/03/02/a-simple-layout-with-two-listviews/
Multiple Listviews in single Activity in Android?
Android how to display 2 listviews in one activity one after the other
回答2:
You can use ListFragment instead ListActivity.
First Fragment:
public class FirstListFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
private List<Tour> tours;
ArrayAdapter<Tour> adapter = new ArrayAdapter<Tour>(this,android.R.layout.simple_list_item_1, tours);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
}
Second fragment:
public class SecondListFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
private List<Tour> tours;
ArrayAdapter<Tour> adapter = new ArrayAdapter<Tour>(this,android.R.layout.simple_list_item_1, tours);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
}
Now you should add fragments to layout in xml file of activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment android:name="com.your_package.FirstListFragment"
android:id="@+id/first_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.your_package.SecondListFragment"
android:id="@+id/second_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
you find mor here: http://www.vogella.com/tutorials/AndroidListView/article.html http://developer.android.com/training/basics/fragments/creating.html
回答3:
You can extend your class with simple Activity and add multiple ListViews in it.
来源:https://stackoverflow.com/questions/24669919/multiple-listview-in-one-activity