【问题标题】:RadioButtons within a RadioGroup check/uncheck randomly in a ListViewRadioGroup 中的 RadioButtons 在 ListView 中随机选中/取消选中
【发布时间】:2017-10-20 09:58:03
【问题描述】:

我无法解决这个问题:我有一个包含 10 个问题的列表视图。对于每个问题,在 RadioGroup 中组织了三个答案(RadioButtons)。问题是每次我滚动列表时,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;}}

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

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>

我知道问题出在适配器的某个地方。一个多星期以来,我一直在努力解决这个问题。如果您有任何建议或解决方案,我将不胜感激。谢谢。

【问题讨论】:

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


    【解决方案1】:

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

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

    不是最干净,但它是工作。

    【讨论】:

      猜你喜欢
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 2013-06-14
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      相关资源
      最近更新 更多