RadioGroup中的RadioButtons在ListView中随机选中/取消选中

时间:2017-10-20 09:58:04

标签: java android listview android-radiogroup android-radiobutton

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

On scrolling List View radio button gets unchecked by itself

If I check a radio button in listview then other radio buttons of listview get checked

Radio Button gets uncked and automatically checked in listview scrolling

Checking RadioButton of RadioGroup in ListView checks other item button

Checkbox unchecked when I scroll listview on 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;}}

以下是我在listview中使用的对象类:

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>

我知道问题是适配器中的问题。我已经试图解决这个问题一个多星期了。如果您有任何建议或解决方案,我将非常感激。谢谢。

1 个答案:

答案 0 :(得分:0)

我的应用程序遇到了同样的问题。我解决了在ViewHolder中保存唯一模型字段。然后我在设置Checkbox之前每次都检查一下,如果模型与ViewHolder具有相同的唯一字段。只有这样,Checkbox设置为正确的条件,如下所示:

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

不是最干净,但它是有效的。