multiple listview in one activity

99封情书 提交于 2019-12-23 04:14:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!