Search code examples
androidlistviewlistactivitytabnavigator

Multiple ListViews with Custom Adapters in a Single Layout


I created a Custom Tab Navigator and now I need to know how can I use multiple ListViews which will receive Custom Adapters.

When I needed to use a Custom Adapter I created a ListView with id=android:list and set the class to Extends ListActivity. But now I think I can't do that...


Solution

  • To have multiple listViews on a single activity, don't need to extend ListActivity. Just add normal ListViews to the xml lauyout file and the reference them on the activity and set the adapters you want.

    Example: xmlfile

    <ListView android:id="@+id/list_view1" android:layout_width="fill_parent"
     android:layout_height="wrap_content">
     </ListView>
    
    <ListView android:id="@+id/list_view2" android:layout_width="fill_parent"
     android:layout_height="wrap_content">
     </ListView>
    

    On the activity:

    setContentView(R.layout.xmlfile)...
    
    ListView lv1 = (ListView) findViewById(R.id.list_view1);
    ListView lv2 = (ListView) findViewById(R.id.list_view2);
    
    lv1.setAdaper(new CustomAdapter1());
    lv2.setAdaper(new CustomAdapter2());