如何在不扩展我的类中的ListActivity的情况下使用listview?

Nul*_*ion 14 android listview

我有一个带有各种标签的应用程序(在tabhost上).每个选项卡都是一个扩展活动的活动,并且包含一些textfields内容.

现在,我需要我的选项卡在listview中,但在Android开发人员指南的示例中,您必须扩展ListActivity而不是Activity.

基本上,我需要合并这两个教程:

如何在listview不延伸listactivity课程的情况下使用?

我的XML文件:

<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>
Run Code Online (Sandbox Code Playgroud)

Mac*_*rse 18

它几乎是一样的.基于ListView的教程.

而不是做:

setListAdapter();
Run Code Online (Sandbox Code Playgroud)

请执行下列操作:

  • <ListView>在您的布局中添加一个
  • 在你的.中创建一个字段var Activity private ListView mListView;

onCreate()方法上这样做:

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

我不记得是否ListActivity提供更多东西.


st0*_*0le 5

你的TabHost也可以包含一个ListActivity,因为它继承自Activity.

但是,如果您想了解如何以任何方式在活动中添加列表视图,请按照这些说明操作.这很简单,Make Activitya Listview,在XML中添加一个.

使用findViewById()让您的 ListView.