2016-04-23 39 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