1

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

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
user3824114
  • 107
  • 1
  • 2
  • 8
  • 1
    then you should not extend ListActivity instead extend it as normal activity and give each listview's unique Id – Kalpesh Lakhani Jul 10 '14 at 06:38
  • ListActivity is specifically for One Single Activity. If you want more than one Listview in an activity. You should just extend Activity class and create multiple listivew. – Ramesh_D Jul 10 '14 at 06:40
  • possible duplicate of [Android how to display 2 listviews in one activity one after the other](http://stackoverflow.com/questions/17693578/android-how-to-display-2-listviews-in-one-activity-one-after-the-other) – ntv1000 Jul 10 '14 at 06:41

3 Answers3

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

Matt Twig
  • 444
  • 3
  • 8
  • give an error(The constructor ArrayAdapter(MainActivity, int, List) is undefined) – user3824114 Jul 10 '14 at 09:06
  • ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, tours); Instead "this" use getActivity() because Fragment object is not child of Context. – Matt Twig Jul 10 '14 at 09:13
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

Community
  • 1
  • 1
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
0

You can extend your class with simple Activity and add multiple ListViews in it.

Nauman Afzaal
  • 1,046
  • 12
  • 20