未选中滚动值的自定义列表视图适配器中的无线电组

Radio Group in custom list view adapter in scrolling value going unchecked

这是我的自定义列表视图 class 当我向下滚动我们的列表视图时,它会未经检查 select 单选按钮

private String[] question_name;
private String[] op11, op22, op33, op44;
private Activity context;
RadioGroup rg=null;
private HashMap<Integer,String>selectitem=new HashMap<>();

public QustionAdapter(Activity context, String[] queation, String[] op1, String[] op2,
                      String[] op3, String[] op4) {
    super(context, R.layout.qustion_custom_list, queation);
    this.context = context;
    this.question_name = queation;
    this.op11 = op1;
    this.op22 = op2;
    this.op33 = op3;
    this.op44 = op4;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = context.getLayoutInflater();

    View listViewItem = inflater.inflate(R.layout.qustion_custom_list, null, false);

    TextView questionname=(TextView)listViewItem.findViewById(R.id.qustion_tb);
    RadioButton op1=(RadioButton)listViewItem.findViewById(R.id.op1);
    RadioButton op2=(RadioButton)listViewItem.findViewById(R.id.op2);
    RadioButton op3=(RadioButton)listViewItem.findViewById(R.id.op3);
    RadioButton op4=(RadioButton)listViewItem.findViewById(R.id.op4);
   rg=(RadioGroup) listViewItem.findViewById(R.id.radio_group);


  //  Toast.makeText(context,"Done :"+rg.getCheckedRadioButtonId(),Toast.LENGTH_LONG).show();
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Model model=new Model();

            RadioButton rb=(RadioButton) group.findViewById(checkedId);
           // Toast.makeText(context,"Done :"+rb.getText(),Toast.LENGTH_LONG).show();
            String sel=rb.getText().toString();
            selectitem.put(position,sel);
            model.setSelectResult(selectitem);
        }
    });


    questionname.setText(question_name[position]);
    op1.setText(op11[position]);
    op2.setText(op22[position]);
    op3.setText(op33[position]);
    op4.setText(op44[position]);

    return listViewItem;
}'

当我 select 第一个单选组按钮开始运行时 select 但是当我回到单选组时滚动它时它没有被选中。

您必须使用布尔值为 true false 的所有对象的模型 class 进行检查和取消检查,并设置 getter 和 setter 方法并访问中的值选中或取消选中 get view() 方法并设置视图的值。

       @Override
       public void onBindViewHolder(final MyViewHolder holder, final inposition)             {     
        final int pos = position;
        holder.title.setText(list.get(pos).getName());
        if (islongpressed)
        {
            holder.checkBox.setVisibility(View.VISIBLE);
        }
        else holder.checkBox.setVisibility(View.GONE);

        holder.checkBox.setChecked(list.get(pos).isSelected());


try {
    holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox cb = (CheckBox) v;

            AudioFile contact =list.get(pos);

            contact.setSelected(cb.isChecked());

            list.get(pos).setSelected(cb.isChecked());

        }
    });


}

我们开始了

    public class CurrentAffairs extends AppCompatActivity {
    public static int correct, wrong, marks;
    DbH db;
    ArrayList<Question> mcqs;
    Cursor cur;
    ListView lv;
    CustomAdapter c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.current_affairs);

        //casting
        lv = (ListView) findViewById(R.id.lv);
        mcqs = new ArrayList<>();


    enter code here
        try {
            db = new DbH(this);
        } catch (IOException e2) {

            e2.printStackTrace();
        }

        try {
            db.createdatabase();
        } catch (IOException e) {

            e.printStackTrace();
        }

        db.opendatabase();
        cur = db.data();

        try {

            while (cur.moveToNext()) {
                String mcqss = cur.getString(1);
                String opta = cur.getString(2);
                String optb = cur.getString(3);
                String optc = cur.getString(4);
                String optd = cur.getString(5);
                String answ = cur.getString(6);

                //put data in a listview
                Question question = new Question();
                question.question = mcqss;
                question.option1 = opta;
                question.option2 = optb;
                question.option3 = optc;
                question.option4 = optd;
                question.correctanxer = answ;
                mcqs.add(question);



               c = new CustomAdapter(CurrentAffairs.this, R.layout.row, R.id.mcqsText, mcqs);
                lv.setAdapter(c);


            }
        } finally {
            cur.close();
        }
        Button btnshow = (Button) findViewById(R.id.btnshowresult);
        btnshow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb = new StringBuffer();
                sb.append("Correct Answer = " + " " + correct);
                sb.append("    " + "Wrong Answer = " + " " + wrong);
                sb.append("    " + "Final Score = " + " " + correct * 5);

                Toast.makeText(CurrentAffairs.this, sb, Toast.LENGTH_SHORT).show();
            }
        });
    }



    class CustomAdapter extends ArrayAdapter<Question> {

        public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<Question> objects) {
            super(context, resource, textViewResourceId, objects);


        }

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

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.row, parent, false);
            TextView mcqsText = (TextView) v.findViewById(R.id.mcqsText);
            TextView ans2 = (TextView) v.findViewById(R.id.answer2);
            TextView anss = (TextView) v.findViewById(R.id.answer);
            RadioGroup rg = (RadioGroup) v.findViewById(R.id.radioGroup);

            RadioButton opt1 = (RadioButton) v.findViewById(R.id.optA);
            RadioButton opt2 = (RadioButton) v.findViewById(R.id.optB);
            RadioButton opt3 = (RadioButton) v.findViewById(R.id.optC);
            RadioButton opt4 = (RadioButton) v.findViewById(R.id.optD);

            Question question = mcqs.get(position);

            mcqsText.setText(question.question);
            opt1.setText(question.option1);
            opt2.setText(question.option2);
            opt3.setText(question.option3);
            opt4.setText(question.option4);
            anss.setText(question.selectedanxer);
            ans2.setText("Correct answer is = " + question.correctanxer);

            String test=opt1.getText().toString();
            String test2=question.selectedanxer+"";

            if (test.equals(""+test2)){
                opt1.setChecked(true);
            }
            if (test2.equals(opt2.getText()+"")){
                opt2.setChecked(true);
            }
            if (test2.equals(opt3.getText()+"")){
                opt3.setChecked(true);
            }
            if (test2.equals(opt4.getText()+"")){
                opt4.setChecked(true);
            }




            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {


                    Question question1=mcqs.get(position);
                    RadioButton radioButton= (RadioButton) group.findViewById(checkedId);
                    mcqs.get(position).selectedanxer=radioButton.getText().toString();
                    notifyDataSetChanged();



                }
            });

            return v;
        }
    }

    public class Question {
        String question;
        String option1;
        String option2;
        String option3;
        String option4;

        String selectedanxer;
        String correctanxer;

    }

}