Radiogroup在android中的listview中

时间:2017-04-19 09:12:04

标签: android listview android-radiogroup

我有一个动态单选按钮的列表视图(它将基于listview中的一些问题的大小创建,即,对于第一个问题我有4个单选按钮第二个问题可能有3个单选按钮.....)。我的问题当用户选择使用单选按钮选择答案时,我需要提交这些表格。那么如何实现这些列表视图数据以及选定的答案。另外我需要检查问题是否得到回答(意味着检查无线电组)。当我提交时,我想要整个数据。任何人都可以帮助我。

这是我的getview

 public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    final ViewHolder viewHolder = new ViewHolder();
    view = View.inflate(mContext, R.layout.item_list_bradenscale_entry, null);

    viewHolder.txtQuestionHeadName = (TextView) view.findViewById(R.id.textview_question_name);
    viewHolder.radioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
    viewHolder.radioButtonOption1 = (RadioButton) view.findViewById(R.id.radio1);
    viewHolder.radioButtonOption2 = (RadioButton) view.findViewById(R.id.radio2);
    viewHolder.radioButtonOption3 = (RadioButton) view.findViewById(R.id.radio3);
    viewHolder.radioButtonOption4 = (RadioButton) view.findViewById(R.id.radio4);
    viewHolder.txtQuestionHeadHint = (TextView) view.findViewById(R.id.textview_question_hint);
    viewHolder.txtOption1Hint = (TextView) view.findViewById(R.id.textview_option1Hint);
    viewHolder.txtOption2Hint = (TextView) view.findViewById(R.id.textview_option2Hint);
    viewHolder.txtOption3Hint = (TextView) view.findViewById(R.id.textview_option3Hint);
    viewHolder.txtOption4Hint = (TextView) view.findViewById(R.id.textview_option4Hint);
    view.setTag(viewHolder);
    selectedList = new 
    final DataContractQuestionList myDataitem = arrData.get(position);
    viewHolder.txtQuestionHeadName.setText(myDataitem.QuestionTitle);
    viewHolder.txtQuestionHeadHint.setText(myDataitem.QuestionHint);


    viewHolder.radioButtonOption1.setText(myDataitem.QuestionOptions.get(0).OptionTitle);
    viewHolder.radioButtonOption2.setText(myDataitem.QuestionOptions.get(1).OptionTitle);
    viewHolder.radioButtonOption3.setText(myDataitem.QuestionOptions.get(2).OptionTitle);
    viewHolder.txtOption1Hint.setText(myDataitem.QuestionOptions.get(0).OptionHint);
    viewHolder.txtOption2Hint.setText(myDataitem.QuestionOptions.get(1).OptionHint);
    viewHolder.txtOption3Hint.setText(myDataitem.QuestionOptions.get(2).OptionHint);
    if (myDataitem.QuestionOptions.size() < 4) {
        viewHolder.radioButtonOption4.setVisibility(View.INVISIBLE);
        viewHolder.txtOption4Hint.setVisibility(View.INVISIBLE);
    } else {
        viewHolder.radioButtonOption4.setText(myDataitem.QuestionOptions.get(3).OptionTitle);
        viewHolder.txtOption4Hint.setText(myDataitem.QuestionOptions.get(3).OptionHint);
        viewHolder.radioButtonOption4.setChecked(myDataitem.QuestionOptions.get(3).checkedStatus);

    }


    viewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int selectedRadioButton = group.getCheckedRadioButtonId();
            View radioButton = group.findViewById(selectedRadioButton);
            int radioId = group.indexOfChild(radioButton);

            if (radioId / 2 == 0) {
                viewHolder.radioButtonOption1.setChecked(true);
                myDataitem.QuestionOptions.get(0).checkedStatus = true;
                myDataitem.QuestionOptions.get(1).checkedStatus = false;
                myDataitem.QuestionOptions.get(2).checkedStatus = false;
                if (myDataitem.QuestionOptions.size() == 4) {

                    myDataitem.QuestionOptions.get(3).checkedStatus = false;
                }
            } else if (radioId / 2 == 1) {
                viewHolder.radioButtonOption2.setChecked(true);
                myDataitem.QuestionOptions.get(0).checkedStatus = false;
                myDataitem.QuestionOptions.get(1).checkedStatus = true;
                myDataitem.QuestionOptions.get(2).checkedStatus = false;
                if (myDataitem.QuestionOptions.size() == 4) {

                    myDataitem.QuestionOptions.get(3).checkedStatus = false;
                }
            } else if (radioId / 2 == 2) {
                viewHolder.radioButtonOption3.setChecked(true);
                myDataitem.QuestionOptions.get(0).checkedStatus = false;
                myDataitem.QuestionOptions.get(1).checkedStatus = false;
                myDataitem.QuestionOptions.get(2).checkedStatus = true;
                if (myDataitem.QuestionOptions.size() == 4) {

                    myDataitem.QuestionOptions.get(3).checkedStatus = false;
                }
            } else if (radioId / 2 == 3) {
                if (myDataitem.QuestionOptions.size() == 4) {
                    viewHolder.radioButtonOption4.setChecked(true);
                    myDataitem.QuestionOptions.get(0).checkedStatus = false;
                    myDataitem.QuestionOptions.get(1).checkedStatus = false;
                    myDataitem.QuestionOptions.get(2).checkedStatus = false;
                    myDataitem.QuestionOptions.get(3).checkedStatus = true;

                }
            }

        }
    });


    viewHolder.radioButtonOption1.setChecked(myDataitem.QuestionOptions.get(0).checkedStatus);
    viewHolder.radioButtonOption2.setChecked(myDataitem.QuestionOptions.get(1).checkedStatus);
    viewHolder.radioButtonOption3.setChecked(myDataitem.QuestionOptions.get(2).checkedStatus);
    return view;
}

1 个答案:

答案 0 :(得分:0)

在适配器 getView()方法中,在呈现列表项时将侦听器附加到单选按钮。在on checked事件中,更改对象的属性。您的活动只能访问相同的对象列表并检索最新的问卷状态。

rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        // This will get the radiobutton that has changed in its check state
        RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
        // This puts the value (true/false) into the variable
        boolean isChecked = checkedRadioButton.isChecked();
        // If the radiobutton that has changed in check state is now checked...
        if (isChecked)
        {
            //Change your object's attribute here
            question.answer = "xxxx";
        }
    }
});