0

我的主要活动有一个包含 ListView 和其他几个项目的布局。我想为它分配一个 ListAdapter。我按照这里显示的教程:http ://www.vogella.com/articles/AndroidListView/article.html

如何让适配器知道我的活动布局中有一个 ListView,并将其分配给它?当我尝试按原样运行应用程序时,它会因运行时异常而崩溃,说明我的布局需要在其中包含默认的 Android 列表视图。

谢谢!

4

2 回答 2

1

如果您的活动继承自ListActivity,则不必手动让适配器知道ListView.

但为了做到这一点,ListView在你的活动中应该是这样的

<ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

因为文件说

您自己的视图必须包含一个 ID 为“@android:id/list”的 ListView 对象(或者如果它在代码中,则为列表)

于 2013-02-13T00:00:02.943 回答
0

当我尝试按原样运行应用程序时,它会因运行时异常而崩溃,说明我的布局需要在其中包含默认的 Android 列表视图。

听起来您正在使用 ListActivity 并调用setContentView(). 在您传递给setContentView()您的布局中,必须具有具有以下属性的 ListView:

<ListView 
    android:id="@android:id/list"
    ... />

然后使用 . 将您的适配器绑定到 ListView setListAdapter()

于 2013-02-12T23:55:56.183 回答