如何检测动态分配的radiobuttons中的点击事件?

时间:2017-09-15 11:49:12

标签: android

我目前正在为学生开发一个应用程序,以查看他们的结果,通知,出勤等。教师也可以通过此应用程序参加考试。 我已经完成了从网上获取学生信息的部分。

由于学生人数多变,我的内容充满活力。

for (int i = 0; i < count; i++) {
            TableRow row = new TableRow(this);
            row.setId(1000 + i);
            //set the color only for the fields in odd places
            if (i % 2 != 0) {
                row.setBackgroundColor(getResources().getColor(R.color.viewSplit));
            }
            row.setGravity(Gravity.CENTER);

            //settting height of each row
            int tt = getResources().getDimensionPixelSize(R.dimen.height);
            row.setMinimumHeight(tt);
            // part1
            TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
            row.setLayoutParams(lp);

            //adding data items in textviews
            int dp = getResources().getDimensionPixelSize(R.dimen.student_name);
            TextView textview1 = new TextView(this);
            textview1.setWidth(dp);
            textview1.setTextColor(Color.BLACK);
            textview1.setGravity(Gravity.CENTER);
            textview1.setText(name_of_student.get(i));

            TextView textview2 = new TextView(this);
            textview2.setTextColor(Color.BLACK);
            dp = getResources().getDimensionPixelSize(R.dimen.roll_no);
            textview2.setWidth(dp);
            textview2.setGravity(Gravity.CENTER);
            textview2.setText(roll_no.get(i));

            //if the student is present color is green
            RadioButton radioButtonPresent = new RadioButton(this);
            radioButtonPresent.setHighlightColor(Color.GREEN);
            dp = getResources().getDimensionPixelSize(R.dimen.present_abscent);
            radioButtonPresent.setWidth(dp);

            //if the student is absentcolor is red
            RadioButton radioButtonAbscent = new RadioButton(this);
            radioButtonAbscent.setHighlightColor(Color.GREEN);
            dp = getResources().getDimensionPixelSize(R.dimen.present_abscent);
            radioButtonAbscent.setWidth(dp);

            row.addView(textview1);
            row.addView(textview2);
            row.addView(radioButtonPresent);
            row.addView(radioButtonAbscent);

            tabLayout.addView(row, i);

所以我制作了两个文本视图来显示学生的姓名和名单。并制作了两个单选按钮,用于选择学生是否缺席或在场。

我可以检测行中的点击事件,因为我为每行设置了ID。 但我遇到的问题是如何检测用户是否按下了当前的单选按钮或没有单选按钮。以及如何获取每个表的哪些字段已被点击。

替代解决方案也会有所帮助

1 个答案:

答案 0 :(得分:0)

添加行时,使用Map将带有ID的学生链接到行的ID。然后在每个单选按钮上定义以下内容:

rBtnAbsent.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // get the element from the map by the id of the row which will fetch the student as well and do the absent function
        }
    });

rBtnPresent.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // get the element from the map by the id of the row which will fetch the student as well and do the present function
        }
    });