2013-02-12 67 views
0

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

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

谢谢!

回答

1

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

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

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

因为documentation says

自己的观点必须包含id为“一个ListView对象@android:ID /列表”(或列表,如果它在代码)

+0

现在我觉得愚蠢。感谢您提供简单有效的答案! – Argus9 2013-02-13 00:16:34

0

当我尝试运行该应用程序时,它的运行时异常崩溃,指出我的布局需要在其中包含默认Android列表视图。

这听起来像您使用的是ListActivity并调用setContentView()。在您传递给setContentView()必须布局具有以下属性一个ListView:

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

那么你的适配器与setListAdapter()绑定到ListView。