【问题标题】:Scrolling issues using RadioGroup in Listview?在 Listview 中使用 RadioGroup 滚动问题?
【发布时间】:2016-04-23 20:57:04
【问题描述】:

在包含列表视图的我的活动中。 Listview 行在 RadioGroup 中包含一个 Textview 和三个 Radiobutton。当我在十 (10) 行后滚动 listView 后选择第一行中的第一个单选按钮时,自动选择的第一个单选按钮出现。这将在每十 (10) 行之后发生,我不知道如何?

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

请任何人帮助我提前谢谢。

【问题讨论】:

标签: android


【解决方案1】:

您的视图持有者将保持视图状态。对于每个项目,您必须将数据保存在适配器中的数组或放入项目对象数据中。

【讨论】:

  • 感谢重播,但我不知道如何实现它。请帮帮我
  • 假设,一个项目显示了 YourData 对象。我可以向这个对象添加一个名为 selectedRadioPosition 的属性,当用户单击收音机时,获取此位置并设置为 selectedRadioPosition。在获取视图中。检查位置并设置检查此位置上的单选按钮
  • 如果可能的话,请告诉我在哪里可以更新我的 java 代码以及如何更新,因为我不知道您要求更改哪种类型,请
  • 向 StudentDataReader 添加一个名为 mSelectedRadio 的 int 属性
  • 好的,我像这样添加了 studentDataReder.setPosition(mSelectedPosition);下一个
猜你喜欢
  • 1970-01-01
  • 2011-06-29
  • 1970-01-01
  • 2016-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 2011-02-19
相关资源
最近更新 更多