2016-04-23 97 views
0

在我的活動中包含列表視圖。 ListView行在RadioGroup中包含一個Textview和三個Radiobutton。當我在滾動listView之後第一行選擇第一個單選按鈕後,十(10)行會自動選擇第一個單選按鈕出現。這將在每十(10)行之後發生,我不知道該怎麼做?在Listview中使用RadioGroup滾動問題?

CustomAdapter類

public class StudentAttendanceCustomAdapter extends BaseAdapter{ 

    private Activity activity; 
    private LayoutInflater inflater; 
    private List<StudentDataReader> studentRecord; 
    private List<StudentDataReader> mOriginalValues; 
    public HashMap<String, String> radioMap; 
    private int mSelectedPosition = -1, radioButtonId; 

    public StudentAttendanceCustomAdapter(TeacherAttendanceActivity activity, List<StudentDataReader> studentList) { 

     this.activity = activity; 
     this.studentRecord = studentList; 
     this.mOriginalValues = studentList; 
    } 

    @Override 
    public int getCount() { 
     return studentRecord.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return studentRecord.get(position); 
    } 

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

    static class ViewHolder{ 
     TextView studentName; 
     RadioButton rb_p; 
     RadioButton rb_a; 
     RadioButton rb_l; 
     RadioGroup rg; 

    } 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     ViewHolder hoder = new ViewHolder(); 
     if (inflater == null) 
     { 
      inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 
     if(convertView == null){ 
      convertView = inflater.inflate(R.layout.teacher_attendance_row,parent,false); 

      hoder.studentName = (TextView)convertView.findViewById(R.id.tv_attendance_studentName); 
      hoder.rg = (RadioGroup)convertView.findViewById(R.id.rg_1); 
      hoder.rb_p = (RadioButton)convertView.findViewById(R.id.radioButton_present); 
      hoder.rb_a = (RadioButton)convertView.findViewById(R.id.radioButton_absent); 
      hoder.rb_l = (RadioButton)convertView.findViewById(R.id.radioButton_leave); 
      convertView.setTag(hoder); 
     }else { 
      hoder = (ViewHolder)convertView.getTag(); 
     } 

     final String str_studentId,str_studentName; 
     final StudentDataReader s = studentRecord.get(position); 
     hoder.studentName.setText(s.getStudentName()); 
     str_studentId= s.getStudentId(); 
     str_studentName = s.getStudentName(); 


     hoder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       int childCount = group.getChildCount(); 

       for (int i = 0; i < childCount; i++) { 
        RadioButton radioButton = (RadioButton) group.getChildAt(i); 

        if (radioButton.getId() == checkedId) { 
         s.setPresent(radioButton.getText().toString()); 
         notifyDataSetChanged(); 
        } 
       } 
      } 
     }); 

     return convertView; 
    } 
} 

請任何一個可以幫助我在此先感謝。

+0

你可以檢查它 - http://stackoverflow.com/a/35862385/2128166 它有不同的數據,但你可以參考你如何能夠保持單選按鈕的狀態。 – androidnoobdev

回答

0

您的視圖持有者將保持視圖狀態。對於每個項目,您必須將數據保存在適配器中,或者將數組放入適配器或放入項目對象數據

+0

感謝重播,但我不知道如何實現它。請幫我 – Shailesh

+0

假設,一個項目顯示YourData對象。我可以爲這個對象添加一個名爲selectedRadioPosition的屬性,當用戶單擊收音機時,獲取此位置並設置爲selectedRadioPosition。在查看。檢查位置和setchecked爲這個位置上的單選按鈕 –

+0

請如果可能然後告訴我在哪裏我可以更新我的java代碼,以及如何,因爲我不知道你請求改變什麼類型請 – Shailesh