开发者

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>


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜