如何在recyclerview中获取所选radiobutton的值?

时间:2016-11-21 13:59:39

标签: android radio-button recycler-adapter radio-group cardview

动态地,我在recycleview中的一个问题下添加了多个选项。现在,用户可以在每个问题中选择一个选项。查看下图, enter image description here

Recyclerview Adapter类是,

 @Override
    public void onBindViewHolder(final MyViewHolder holder,int position) {
        final PollQstWithAns poll = dataList.get(position);

        holder.txt_poll_question.setText(poll.getPollQstName());

        for (int i = 0; i < poll.getOptionList().size(); i++) {
            final PollsData mPollsOptionsList = poll.getOptionList().get(i);

            final RadioButton rb = new RadioButton(context);
            rb.setText(mPollsOptionsList.getAns1());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                ColorStateList colorStateList = new ColorStateList(
                        new int[][]{

                                new int[]{-android.R.attr.state_enabled},
                                new int[]{android.R.attr.state_enabled}
                        },
                        new int[]{

                                Color.DKGRAY
                                , ContextCompat.getColor(context, R.color.colorPrimary)

                        }
                );
                rb.setButtonTintList(colorStateList);
                rb.invalidate();
                rb.setTextColor(ContextCompat.getColor(context, R.color.gray));
            }

            holder.optionRadioGroup.addView(rb);

        }
    }

如果用户选择RadioButton内的RadioGroup,我想从每个问题中获取所有选中的值。

1 个答案:

答案 0 :(得分:1)

根据您希望使用信息的时间,有两种方法可以实现。

如果您想等待用户检查所有问题然后获取值(例如发送答案按钮或类似内容),您可以使用RadioGroup对象中的getCheckedRadioButtonId()。

如果您想立即使用信息(当用户选择它时),您可以在无线电组对象上添加一个监听器:无线电组对象上的setOnCheckedChangeListener()。

相关问题