2017-04-19 58 views
0

我有一個列表視圖與動態單選按鈕(它將創建基於大小的一些問題,在第一個問題我有4個單選按鈕第二個問題可能有3個單選按鈕... ..)。我的問題是當用戶選擇使用單選按鈕的答案我需要提交這些表單。那麼我怎樣才能實現這些列表視圖數據以及選定的答案。我還需要檢查問題是否回答(表示檢查收音機組)。當我提交我想要的整個數據。任何人都可以幫助我。Radiogroup在Android的列表視圖

這是我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; 
} 
+0

使用相同的數組來存儲選定的狀態。然後根據狀態,您可以獲取檢查的數據併發送。 –

+0

發表我的getview() – Sri

回答

0

在適配器getView()方法,重視聽者的單選按鈕,同時渲染你的列表項。在檢查的事件中,更改對象的屬性。您的活動只能訪問相同的對象列表並檢索最新的問卷狀態。

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"; 
     } 
    } 
}); 
+0

如果問題未得到解答,rGroup.setOnCheckedChangeListener()將如何調用? – Sri

+0

如果它沒有被回答,只是保持問題的默認狀態,例如回答=錯誤。它不工作嗎? –