在 android 的列表视图中滚动时未选择单选按钮

Radio Button not selected when scroll in listview in android

当我在 android ListView, RadioButton 中向下滚动时无法选择。我将 set selected true 应用于 set change check listener,但无法正常工作,需要帮​​助。谢谢提前。 我使用一个使用 4 RadioButton 并设置 mcq answer option 的自定义适配器。 50 mcq 但在 ListView 中滚动时比未选择上一个项目。自动刷新。谁能建议我该怎么做。

public class ModeltestDetailsAdapter 扩展 ArrayAdapter {

private final Context context;
private final List<AllData> values;
public static ArrayList<String> selectedAnswers;

public ModeltestDetailsAdapter(Context context, List<AllData> values) {

    super(context, R.layout.list_details_modeltest, values);
    this.context = context;
    this.values = values;

    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < values.size(); i++) {
        selectedAnswers.add("Not Attempted");
    }

}

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

    // urveyDataHelper mDbHelper = new SurveyDataHelper(context);

    final ViewGroup p;
    p = parent;

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.list_details_modeltest, parent, false);

    TextView questions = (TextView) rowView.findViewById(R.id.questions);
    TextView answer = (TextView) rowView.findViewById(R.id.ans);
    final RadioGroup radioGroup = (RadioGroup) rowView.findViewById(R.id.radiogroup);
    RadioButton radioButton;
    String result;
    final RadioButton optiona= (RadioButton) rowView.findViewById(R.id.optiona);
    final RadioButton   optionb= (RadioButton) rowView.findViewById(R.id.optionb);
    RadioButton optionc= (RadioButton) rowView.findViewById(R.id.optionc);
    RadioButton optiond= (RadioButton) rowView.findViewById(R.id.optiond);

    questions.setText(values.get(position).getQues());
    answer.setText(values.get(position).getAns());
    optiona.setText(values.get(position).getOpt_one());
    optionb.setText(values.get(position).getOpt_two());
    optionc.setText(values.get(position).getOpt_three());
    optiond.setText(values.get(position).getOpt_four());

    optionc.setChecked(true);



    optiona.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // set Yes values in ArrayList if RadioButton is checked
            if (isChecked) {
                optiona.setChecked(true);

                //notifyDataSetChanged();
                Toast.makeText(
                        context,
                        "Answer is ." + buttonView.getText().toString(),
                        Toast.LENGTH_LONG).show();

                if (values.get(position).getAns().equals(buttonView.getText().toString())) {

                    selectedAnswers.set(position, "Correct");

                } else {
                    selectedAnswers.set(position, "Wrong");
                }

            }else {
                optiona.setChecked(false);
            }


        }
    });



    optionb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set Yes values in ArrayList if RadioButton is checked
            if (isChecked) {

                optionb.setChecked(true);

                // notifyDataSetChanged();
                Toast.makeText(
                        context,
                        "Answer is ." + buttonView.getText().toString(),
                        Toast.LENGTH_LONG).show();

                if (values.get(position).getAns().equals(buttonView.getText().toString())) {

                    selectedAnswers.set(position, "Correct");

                } else {
                    selectedAnswers.set(position, "Wrong");
                }

            }
            else {
                optionb.setChecked(false);
            }
        }

    });



    questions.setTag(position);
    answer.setTag(position);
    optiona.setTag(position);
    optionb.setTag(position);
    optionc.setTag(position);
    optiond.setTag(position);
    //radioButton.setTag(position);
    optiona.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final int pos = (Integer) v.getTag();

            optiona.setChecked(true);
            optiona.setSelected(true);

            ((ListView) p).performItemClick(v, pos, 0);
        }
    });


    answer.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final int pos = (Integer) v.getTag();

            ((ListView) p).performItemClick(v, pos, 0);
        }
    });

    questions.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final int pos = (Integer) v.getTag();

            ((ListView) p).performItemClick(v, pos, 0);
        }
    });

    return rowView;
}

}

在您的 AllData 模型中,采用另一个变量,

int selectedAnswer = -1; // -1 as default for not selected answer.

现在,在您的适配器 class 中,在 getView() 方法中,执行以下实现。

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

    final ViewGroup p;
    p = parent;

    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.list_details_modeltest, parent, false);

    TextView questions = (TextView) rowView.findViewById(R.id.questions);
    TextView answer = (TextView) rowView.findViewById(R.id.ans);
    final RadioGroup radioGroup = (RadioGroup) rowView.findViewById(R.id.radiogroup);
    RadioButton radioButton;
    String result;
    final RadioButton optiona= (RadioButton) rowView.findViewById(R.id.optiona);
    final RadioButton   optionb= (RadioButton) rowView.findViewById(R.id.optionb);
    RadioButton optionc= (RadioButton) rowView.findViewById(R.id.optionc);
    RadioButton optiond= (RadioButton) rowView.findViewById(R.id.optiond);

    questions.setText(values.get(position).getQues());
    answer.setText(values.get(position).getAns());
    optiona.setText(values.get(position).getOpt_one());
    optionb.setText(values.get(position).getOpt_two());
    optionc.setText(values.get(position).getOpt_three());
    optiond.setText(values.get(position).getOpt_four());

    if(values.get(position).getSelectedAnswer() == 1){
        optiona.setSelected(true);
    }
    else if(values.get(position).getSelectedAnswer() == 2){
        optionb.setSelected(true);
    }
    else if(values.get(position).getSelectedAnswer() == 3){
        optionc.setSelected(true);
    }
    else if(values.get(position).getSelectedAnswer() == 4){
        optiond.setSelected(true);
    }

    optiona.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked) {
            values.get(position).setSelectedAnswer(1);
            ....

        }else {
            optiona.setChecked(false);
            }


        }
    });

    ....

    return rowView;
}