Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在listview中选择适当的复选框_Android_Listview_Checkbox - Fatal编程技术网

Android 如何在listview中选择适当的复选框

Android 如何在listview中选择适当的复选框,android,listview,checkbox,Android,Listview,Checkbox,请帮忙,这个(可能很简单的)问题让我发疯 我有一个包含textview和checkbox的自定义列表视图。 我想在选中复选框时对listview元素执行操作。我正在尝试使用下面的代码,但问题是我不知道如何检测单击了哪个特定的复选框 checkBox = (CheckBox) findViewById(R.id.textCheckbox); checkBox.setOnCheckedChangeListener(this); public void onCheckedChanged(Compou

请帮忙,这个(可能很简单的)问题让我发疯

我有一个包含textview和checkbox的自定义列表视图。 我想在选中复选框时对listview元素执行操作。我正在尝试使用下面的代码,但问题是我不知道如何检测单击了哪个特定的复选框

checkBox = (CheckBox) findViewById(R.id.textCheckbox);
checkBox.setOnCheckedChangeListener(this);

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked)
        make some action
    else
        make another action
}
我还尝试使复选框对OnItemLongClickListener作出反应:

OnItemLongClickListener LongListener = new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> a, View v, int position, long id) {
        CheckBox checkBox = (CheckBox) v.findViewById(R.id.textCheckbox);
        listView = (ListView) a;
        checkBox.setChecked(true);
        return true;
    }
};
OnItemLongClickListener LongListener=新建OnItemLongClickListener(){
@凌驾
长点击(AdapterView a、视图v、整型位置、长id){
CheckBox=(CheckBox)v.findViewById(R.id.textCheckbox);
listView=(listView)a;
checkBox.setChecked(true);
返回true;
}
};
这样,当我在listview项目上长按时,我可以选中一个适当的复选框,但只要我向下滚动列表,复选框就会从它的位置移动


请指教

最好的方法是更新基础数据并调用
notifyDatasetChanged()

OnItemLongClickListener LongListener=新建OnItemLongClickListener(){
@凌驾
长点击(AdapterView a、视图v、整型位置、长id){
someListWithYourData.get(position).setSomething(true);//现在您的数据反映了更改
yourAdapter.notifyDataSetChanged();
返回true;
}
};

您在行中使用的是什么布局,它是否实现可检查?我同意您的看法@Sam@Sam我使用包含复选框和文本视图的自定义布局。感谢您如此迅速的回答!如果我写listView.get(position).setSomething(true);我得到一个错误,“类型ListView的方法get(int)未定义”。我能做什么?还有一个愚蠢的问题:“设置某物”是什么意思?setChecked?在哪些数据上构建ListView?您要将哪些数据提供给适配器?我有一个包含数据的arraylist
OnItemLongClickListener LongListener = new OnItemLongClickListener() {
@Override
    public boolean onItemLongClick(AdapterView<?> a, View v, int position, long id) {
        someListWithYourData.get(position).setSomething(true); // Now your data reflects the change
        yourAdapter.notifyDataSetChanged();
        return true;
    }
};