Android的复选框multiselected问题 [英] Android checkbox multiselected issue
问题描述
我做的ListView用组行。有一个复选框,每行。如果我点击一个复选框,另一个复选框还检查。我将做什么,以避免这种情况?任何人都知道帮我..
I did listview with set of rows. Each rows having a checkbox. If I am clicking one checkbox, another checkbox also checked. What I will do to avoid this ? Anyone knows help me..
推荐答案
要避免在ListView具有的CheckBox的问题,你可以采取一个布尔数组初始化在开始false,然后如实相应的位置上排列在复选框在ListView检查。这不会引起麻烦的复选框选中状态,当你向前和向后移动的应用程序或滚动的ListView。
To avoid the problems with CheckBoxes in a ListView you can take a Boolean array initialized false in the beginning and then making true the corresponding position in the array where checkbox is checked in the ListView. This will not cause a trouble in checked state of checkboxes when you move forward and backward in your application or scroll the ListView.
下面是如何设置checkboxstate是布尔数组:
Here is how to set checkboxstate is the boolean array:
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked())
checkBoxState[pos] = true;
else
checkBoxState[pos] = false;
}
});
和那么这将保持复选框选中,当你滚动状态也不会自动改变:
and then this will keep the checkboxes checked when you scroll and the state will not automatically change:
holder.checkbox.setChecked(checkBoxState[pos]);
这篇关于Android的复选框multiselected问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!