单选组中的单选按钮在列表视图中随机选中/取消选中



我无法解决这个问题:我有一个包含10个问题的列表视图。对于每个问题,都有三个答案(单选按钮),这些答案组织在一个单选组中。问题是,每次我滚动列表时,单选按钮都会随机选中/取消选中。我知道我必须以某种方式保存答案,并在每次滚动列表时"重新加载"它们,但我就是无法让它发挥作用。我已经在互联网上搜索并研究了这些主题:

滚动"列表视图"单选按钮会自动取消选中 如果我在列表视图中检查一个单选按钮,那么列表视图的其他单选按钮会被检查 单选按钮被解开,并在列表视图滚动中自动检查 检查ListView中RadioGroup的RadioButton检查其他项目按钮 当我在Android 上滚动列表视图时,复选框未选中 这是我的适配器代码:
public class adapterEdit extends ArrayAdapter<AnswerModel> {
private final ArrayList<AnswerModel> mQuestions;
private final Activity mContext;


public adapterEdit(Activity context, ArrayList<AnswerModel> questions){
super(context,0,questions);
this.mQuestions = questions;
this.mContext = context;

}
static class ViewHolder {
protected TextView text;
protected RadioButton one;
protected RadioButton two;
protected RadioButton three;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;
if( convertView == null) {
LayoutInflater inflater = mContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textOfQuestion);
holder.one = (RadioButton) convertView.findViewById(R.id.answerOne);
holder.one.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer)buttonView.getTag();
mQuestions.get(getPosition).setSelected(buttonView.isChecked());
}
});
holder.two = (RadioButton) convertView.findViewById(R.id.answerTwo);
holder.two.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer)buttonView.getTag();
mQuestions.get(getPosition).setSelected(buttonView.isChecked());
}
});
holder.three = (RadioButton) convertView.findViewById(R.id.answerThree);
holder.three.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer)buttonView.getTag();
mQuestions.get(getPosition).setSelected(buttonView.isChecked());
}
});

convertView.setTag(holder);
convertView.setTag(R.id.answerOne, holder.one);
convertView.setTag(R.id.answerTwo, holder.two);
convertView.setTag(R.id.answerThree, holder.three);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.one.setTag(position);
holder.two.setTag(position);
holder.three.setTag(position);

holder.text.setText(mQuestions.get(position).getQuestion());
holder.one.setText(mQuestions.get(position).getAnswerOne());
holder.one.setChecked(mQuestions.get(position).isSelected());
holder.two.setText(mQuestions.get(position).getAnswerTwo());
holder.two.setChecked(mQuestions.get(position).isSelected());
holder.three.setText(mQuestions.get(position).getAnswerThree());
holder.three.setChecked(mQuestions.get(position).isSelected());


return convertView;}}

这是我在列表视图中使用的对象的类:

public class AnswerModel {
private String mQuestion;
private String mAnswerOne;
private String mAnswerTwo;
private String mAnswerThree;
private boolean selected = false;

public AnswerModel (String question, String answerOne, String answerTwo, String answerThree) {
this.mQuestion = question;
this.mAnswerOne = answerOne;
this.mAnswerTwo = answerTwo;
this.mAnswerThree = answerThree;
}
public String getQuestion(){return mQuestion;}
public String getAnswerOne(){return mAnswerOne;}
public String getAnswerTwo(){return mAnswerTwo;}
public String getAnswerThree(){return mAnswerThree;}
public boolean isSelected() {return selected;}
public void setSelected(boolean selected) {
this.selected = selected;
}}

以及列表项的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.harrypotterfanquiz.MainActivity">

<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textOfQuestion"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_marginTop="10dp"
android:background="@color/questionColor"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/questionOne"
android:textAppearance="@style/appearanceBody" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
<RadioButton
android:id="@+id/answerOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/questionOneA" />
<RadioButton
android:id="@+id/answerTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/questionOneB" />
<RadioButton
android:id="@+id/answerThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/questionOneC" />
</RadioGroup>
</LinearLayout>

我知道问题出在适配器的某个地方。我已经试着解决这个问题一个多星期了。如果你有任何建议或解决方案,我将不胜感激。非常感谢。

我的应用程序也遇到了同样的问题。我通过在ViewHolder中保存唯一的模型字段来解决问题。然后,每次设置复选框之前,我都要检查模型是否与ViewHolder具有相同的唯一字段。直到那时,复选框才被设置为正确的条件,如下所示:

if(holder.uniqueField == mQuestions.get(position).uniqueField) {
holder.one.setChecked(mQuestions.get(position).isSelected());
}

不是最干净的,但这是工作。

最新更新