如何在 LinearList 视图中存储组单选按钮的状态

How to store the state of the group radio button in a LinearList View

我有一个线性布局的调查问卷和一个单选按钮组。现在,当我向下滚动线性列表并再次向上滚动时,单选按钮的选定状态变为默认状态(即未选择任何内容)。如何存储单选按钮的状态。请注意,我已尝试使用共享首选项方法!不起作用! 这是我的代码:

 class MyCustomAdapter extends BaseAdapter {
              Vector<String> question;
              Vector<String> answerA;
              Vector<String> answerB;
              Vector<String> answerC;
              Vector<String> answerD;

              MyCustomAdapter() {
                     question = null;
                     answerA = null;
                     answerB = null;
                     answerC = null;
                     answerD = null;

              }

              MyCustomAdapter(Vector<String> qs, Vector<String> ansA,Vector<String> ansB,Vector<String> ansC,Vector<String> ansD) {
                     question = qs;
                     answerA = ansA;
                     answerB = ansB;
                     answerC = ansC;
                     answerD = ansD;
                     // data_image = image;
              }

              public int getCount() {
                     return question.size();
              }

              public String getItem(int position) {
                     return null;
              }

              public long getItemId(int position) {
                     return position;
              }

              public View getView(final int position, View convertView,
                           ViewGroup parent) {
                     LayoutInflater inflater = getLayoutInflater();
                     View row=null;
                     row = inflater.inflate(R.layout.accountlist, parent, false);
                     TextView tv = (TextView) row.findViewById(R.id.questionText);
                     tv.setText(question.get(position).toString());



                     System.out.println("--------------------------------------");

                     rg=(RadioGroup)row.findViewById(R.id.radio);
                     RadioButton rba=(RadioButton)row.findViewById(R.id.radioA);
                     RadioButton rbb=(RadioButton)row.findViewById(R.id.radioB);
                     RadioButton rbc=(RadioButton)row.findViewById(R.id.radioC);
                     RadioButton rbd=(RadioButton)row.findViewById(R.id.radioD);

                  rba.setText(answerA.elementAt(position));
                     rbb.setText(answerB.elementAt(position));
                     rbc.setText(answerC.elementAt(position));
                     rbd.setText(answerD.elementAt(position));



                     if(position==1){
                           rba.setChecked(true);
                     }




                     rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                   @Override
                   public void onCheckedChanged(RadioGroup group, int checkedId) {
                       RadioButton rb = (RadioButton) group.findViewById(checkedId);
                       if(null!=rb && checkedId > -1){

//                         Toast.makeText(ContestActivity.this, rb.getText()+""+position, Toast.LENGTH_SHORT).show();
                            System.out.println("*****************************************");
                            if(position==0){


                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans1=questionID.elementAt(position)+":"+id;
                            }if(position==1){    
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans2=questionID.elementAt(position)+":"+id;
                            }if(position==2){  
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans3=questionID.elementAt(position)+":"+id;
                            }if(position==3){ 
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans4=questionID.elementAt(position)+":"+id;
                            }if(position==4){   
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans5=questionID.elementAt(position)+":"+id;
                            }if(position==5){ 
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans6=questionID.elementAt(position)+":"+id;
                            }if(position==6){ 
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans7=questionID.elementAt(position)+":"+id;
                            }if(position==7){ 
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans8=questionID.elementAt(position)+":"+id;
                            }if(position==8){  
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans9=questionID.elementAt(position)+":"+id;
                            }if(position==9){ 
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans10=questionID.elementAt(position)+":"+id;
                            }if(position==10){ 
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans11=questionID.elementAt(position)+":"+id;
                            }if(position==11){  
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans12=questionID.elementAt(position)+":"+id;
                            }if(position==12){  
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans13=questionID.elementAt(position)+":"+id;
                            }if(position==13){ 
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans14=questionID.elementAt(position)+":"+id;
                            }if(position==14){  
                                  int index=answerValue.indexOf(rb.getText());
                                  String id=answerID.elementAt(index);
                                  ans15=questionID.elementAt(position)+":"+id;
                            }
                       }
                   }
               });


                 return (row);
          }

您需要在代码

下方创建一个数组并存储单选按钮的状态 state.see
private boolean[] myChecks = new boolean[question.size()];
private class MyCustomAdapter extends BaseAdapter {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ...
    rg.setChecked(myChecks[position]);
    rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

            myChecks[position] = arg1;


           /// put your code/////
        }
    });
}