禁用的复选框在通过android应用程序中的适配器回收视图后被随机启用
我有以下问题。我在我的应用程序中设置了三个复选框,当我选中其中一个复选框时,其他两个复选框将被禁用。问题是,当我向上或向下滚动时,即当视图被回收时,一些禁用的复选框被启用,这当然是我不希望发生的。你能帮我解决那个问题吗 适配器中的代码如下所示:禁用的复选框在通过android应用程序中的适配器回收视图后被随机启用,android,Android,我有以下问题。我在我的应用程序中设置了三个复选框,当我选中其中一个复选框时,其他两个复选框将被禁用。问题是,当我向上或向下滚动时,即当视图被回收时,一些禁用的复选框被启用,这当然是我不希望发生的。你能帮我解决那个问题吗 适配器中的代码如下所示: @NonNull @Override public View getView(final int position1, View convertView1, ViewGroup parent1) { View l
@NonNull
@Override
public View getView(final int position1, View convertView1, ViewGroup parent1) {
View listItemView1 = convertView1;
if (listItemView1 == null) {
listItemView1 = LayoutInflater.from(getContext()).inflate(R.layout.list_item1, parent1, false);
}
final ColorQuiz currentColorQuiz = getItem(position1);
TextView questionTextView = (TextView) listItemView1.findViewById(question_text_view);
questionTextView.setText(currentColorQuiz.getQuestionHeader());
final CheckBox box1 = (CheckBox) listItemView1.findViewById(R.id.check_Box_view1);
box1.setText(currentColorQuiz.getCheckBoxTextA());
final CheckBox box2 = (CheckBox) listItemView1.findViewById(R.id.check_Box_view2);
box2.setText(currentColorQuiz.getCheckBoxTextB());
final CheckBox box3 = (CheckBox) listItemView1.findViewById(R.id.check_Box_view3);
box3.setText(currentColorQuiz.getCheckBoxTextC());
box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (box1.isChecked() ) {
thesisA[position1]=true;
box2.setEnabled(false);
box3.setEnabled(false);
} else {
thesisA[position1]=false;
box2.setEnabled(true);
box3.setEnabled(true);
}
int getPositionA = (Integer) compoundButton.getTag(); // Here we get the position that we have set for the checkbox using setTag.
colorQuizs.get(getPositionA).setSelectedA(compoundButton.isChecked());
}
});
box1.setTag(position1);
box1.setChecked(colorQuizs.get(position1).isSelectedA());
。
.
.
并以同样的方式继续下两个复选框(box2,box3)。可能在您调用
box1.setChecked
时,调用了一个旧的侦听器,它导致了问题(因为它仍然有旧的标记,并且可能为您的模型设置了错误的数据)
试着在乞讨时添加以下内容,以确保不会发生错误:
box1.setOnCheckedChangeListener(null);
box2.setOnCheckedChangeListener(null);
box3.setOnCheckedChangeListener(null);
这将告诉您在属性未更改时不调用回调是否是此问题的原因:
box1.setChecked(!colorQuizs.get(position1).isSelectedA());
box1.setChecked(colorQuizs.get(position1).isSelectedA());
在我的课堂上,每个复选框都有不同的“标签”。正如您所注意到的,我有一个布尔值isSelectedA和一个void setSelectedA(这意味着对于box2,我有一个布尔值isSelectedB和一个void SetSelectedB,依此类推…)。在上下滚动时,我对复选框没有问题。问题出在未选中的复选框中,因此被禁用。。。不过我会试试你的建议。如果你调用setChecked(true)并且它已经被选中,那么回调将不会被调用,因此,设置此属性时也应该更新模型。我刚刚检查了您的建议,并将三行代码放在第一个侦听器之前,但不幸的是问题仍然存在。很抱歉,如果调用setChecked(),我不理解您的意思。。。你能给我解释一下吗,或者给我建议一个例子或者一些代码吗?我的意思是这行
box1.setChecked(colorQuizs.get(position1.isSelectedA())代码>如果它被选中,例如,因为在最后一项中它被选中,并且它正在被重用,那么回调将不会被调用,因为没有任何更改。