Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a listview without extending ListActivity in my class?

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:

  • List View

  • Layouts

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>
like image 215
NullPointerException Avatar asked Nov 09 '10 12:11

NullPointerException


People also ask

How do you dynamically add elements to a ListView on android?

This example demonstrates how do I dynamically add elements in ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

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.

like image 113
Macarse Avatar answered Oct 16 '22 18:10

Macarse