14

I have an application with various tabs (on a tabhost). Each tab is an activity that extends activity and has some textfields and things on it.

Now, I need that my tabs have inside a listview, but in the example from Android developer guide says that you have to extend ListActivity and not Activity.

Basically, I need to merge these two tutorials:

How can I use a listview without extending listactivity on my class?

My XML file:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"/>
    </LinearLayout>
</TabHost>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

2 Answers2

18

It's almost the same. Basing myself on ListView's tutorial.

Instead of doing:

setListAdapter();

do the following:

  • Add a <ListView> in your layout
  • create a field var in your Activity private ListView mListView;

on the onCreate() method do this:

 mListView = (ListView) findViewById(R.id.your_list_view_id);
 mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

I don't remember if ListActivity provides something more.

Macarse
  • 91,829
  • 44
  • 175
  • 230
5

Your TabHost can contain a ListActivity as well, since it inherits from Activity.

But, in case you want learn how to add a listview in an activity any way, follow these instructions. It's simple enough, Make an Activity, Add a Listview in your XML.

Use findViewById() to get your ListView.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
st0le
  • 33,375
  • 8
  • 89
  • 89
  • I am trying to do also with this way but i get an error too: 11-09 19:13:43.678: ERROR/AndroidRuntime(936): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' – NullPointerException Nov 09 '10 at 19:17
  • 1
    @AndroidUser99, when using a `listactivity`, your xml must always have a listview whose `id` is `@android:id/list` – st0le Nov 10 '10 at 04:44