java 如何获得 setOnCheckedChangeListener (ListView Checkboxes to work)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7417105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the setOnCheckedChangeListener (ListView Checkboxes to work)
提问by Tiago
I flagged the other thread to be deleted, because the main question was edited so many times, that it become confusing.
我标记了另一个线程要删除,因为主要问题被编辑了很多次,以至于变得混乱。
So the problem is: I want to populate a ListView with checkboxes, and as I want to customize my ListView I'm not using simple_multiple_choice_mode, I'm putting my layout on list_item.xml: (for each row)
所以问题是:我想用复选框填充 ListView,因为我想自定义我的 ListView 我没有使用 simple_multiple_choice_mode,我将我的布局放在 list_item.xml 上:(对于每一行)
<CheckBox>
xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="8mm"
android:layout_width="fill_parent"
android:layout_height="wrap_removed" android:id="@+id/nomeAPP" style="?listItem">
</<CheckBox>
My ListView is on lista.xml
我的 ListView 在 lista.xml 上
<ListView android:id="@android:id/list"
android:layout_width="fill_parent" android:layout_height="wrap_content"
style="?whiteBackground">
my adapter:
我的适配器:
list=getListView();
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.list_item, aux))
So I'm trying to make a setOnCheckedChangeListener for the checkboxes, so I can store the chosed items in an array.
所以我正在尝试为复选框创建一个 setOnCheckedChangeListener,这样我就可以将所选项目存储在一个数组中。
So I did this listener :
所以我做了这个听众:
CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
Toast.makeText(getBaseContext(), "Checked",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "UnChecked",
Toast.LENGTH_SHORT).show();
}
}
});
Problem is: I get the NULL exception. The CURIOUSthing is: if I "switch" my layout for a simple
问题是:我收到 NULL 异常。在好奇的一点是:如果我的“开关”我的布局简单
<ScrollView android:id="@+id/widget54" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<CheckBox android:text="Checkbox" android:id="@+id/CheckBox01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</LinearLayout>
</ScrollView>
And forget my ListView (and my adapter), it works! So, what's wrong with my layout?
忘记我的 ListView(和我的适配器),它有效!那么,我的布局有什么问题?
I created a new little project just to let you guys see exactly the situation. It's available HERERight now does not work, but if you uncomment the adapter, change ListActivity to Activity and change setContentView, it will work (checked/unchecked toast text).
我创建了一个新的小项目,只是为了让你们准确了解情况。它在此处可用现在不起作用,但如果您取消注释适配器,将 ListActivity 更改为 Activity 并更改 setContentView,它将起作用(选中/未选中的吐司文本)。
Another curious thing I found is that with ListActivity does not work never.... wondering if it's related...
我发现的另一个奇怪的事情是 ListActivity 永远不会工作......想知道它是否相关......
采纳答案by Yashwanth Kumar
ok, i think i got your problem,
好的,我想我遇到了你的问题,
CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01);
This works in the second layout, because you might have already set the view for the activity using setcontentview().
这适用于第二个布局,因为您可能已经使用 setcontentview() 为活动设置了视图。
So , the problem in your list activity is that , the above mentioned line doesn't know , which layout it has to look for to find id,
因此,您的列表活动中的问题在于,上面提到的行不知道它必须寻找哪个布局才能找到 id,
R.id.CheckBox01
you have to get access to the layout view which you are inflating the listview , lets call it a inflatedView
, and then you access the check box like this.
你必须访问你正在膨胀列表视图的布局视图,让我们称之为 a inflatedView
,然后你像这样访问复选框。
CheckBox cb = (CheckBox) inflatedView.findViewById(R.id.CheckBox01);
if you don't set the content view, you have to tell the function findViewById(), where it has to look for the view. I would confirm this, if you post the NPE log.
如果您不设置内容视图,则必须告诉函数 findViewById(),它必须在何处查找视图。如果您发布 NPE 日志,我会确认这一点。
HTH.
哈。