1

我在線性佈局和單選按鈕組中有調查問卷。現在,當我向下滾動線性列表並再次向上滾動時,單選按鈕的選定狀態變爲默認狀態(即沒有選擇任何選項)。我如何存儲單選按鈕的狀態。而且只是一個人頭,我已經嘗試過使用Shared偏好方法!不起作用! 這裏是我的代碼:如何在線性列表視圖中存儲組單選按鈕的狀態

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); 
      } 

回答

2

您需要創建下面的代碼的單選按鈕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///// 
     } 
    }); 
} 
相關問題