Android 按钮下的ListView
因此,我尝试在同一布局的按钮下填充ListView。基本上,当选择ListView选项时,按钮将对每个选择的选项做出不同的响应。问题是它正在接近一个强制,并且似乎是填充ListView的问题Android 按钮下的ListView,android,listview,Android,Listview,因此,我尝试在同一布局的按钮下填充ListView。基本上,当选择ListView选项时,按钮将对每个选择的选项做出不同的响应。问题是它正在接近一个强制,并且似乎是填充ListView的问题 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vert
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
>
<Button
android:background="@drawable/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_gravity="center"
android:id="@+id/button"/>
<ListView
android:listSelector="#990000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:id="@+id/listview"
/>
</LinearLayout>
我已经试过了ArrayAdapter中的内容。。。(android.R.id.list。我尝试过许多其他方法,包括extends-ListActivity,但不管我尝试了什么,我似乎都得到了类似的Logcat响应
有没有更好的方法来完成我想要完成的任务?我希望ListView上面的按钮都在同一个main.xml上
提前谢谢
编辑:
我也试过这段代码。现在注释掉了,但这是我最初尝试的
setListAdapter(new ArrayAdapter<String>(this, android.R.id.list,LIST));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
setListAdapter(新的ArrayAdapter(this,android.R.id.list,list));
ListView ListView=getListView();
setTextFilterEnabled(true);
LIST是一个字符串数组。上面代码在Logcat上的输出与前面代码相同。听起来好像您正在尝试实现ListActivity或ListFragment,它希望找到具有特定名称的listview实例,因此出现错误。请尝试从以下位置更改listview的id:
android:id="@+id/listview"
致:
我知道你说你已经试过了,但是根据你的代码,它看起来仍然是错误的。一旦你改变了它,你能发布新的错误消息吗?根据你的描述,我意识到这将是一条类似的消息,但即使差别很小,它可能仍然很重要
编辑:
我刚刚注意到的另一件事——如果您确实在使用ListActivity或FragmentActivity,请通过调用
getListView()而不是findViewById()`,来获取ListView的引用。感谢您的响应。是的,我尝试了一下,然后再次尝试。将其更改为,然后尝试将lv findViewById更改为(android.R.id.list)并编译得到相同的错误。然后将ArrayAdapters更改为(android.R.id.list)并使用相同的错误。下面是在second08-23 10:51:05.215:error/AndroidRuntime(651)之后在Logcat上的输出:原因:java.lang.RuntimeException:您的内容必须有一个id属性为“android.R.id.list”的ListView。您能确认您正在使用ListActivity或ListFragment吗?看起来确实如此。如果是这样,请参阅我上面关于使用getListView()而不是findViewById的附加说明。上面发布的代码是我在使用getListView()之前使用的代码。当我尝试该代码时,我将其设置为extends ListActivity。目前我正在尝试仅扩展Activity LogCat错误描述了如何修复自身,Nick提供了更详细的答案。我建议将id
更改为Nick的代码。
setListAdapter(new ArrayAdapter<String>(this, android.R.id.list,LIST));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
android:id="@+id/listview"
android:id="@android:id/list"