I am trying to get the value of a radio button that I add to a radio group and that gets added to a linear layout, and then I call the class from my activity. This is my code:
MultiChoice.java
public class MultipleChoice {
Context context;
List<String> choice_values;
String hint;
public MultipleChoice (Context context, String hint, List<String> choice_value_array){
choice_values = new ArrayList<String>();
this.context = context;
this.choice_values = choice_value_array;
this.hint = hint;
}
public View createRadioGroup(){
LinearLayout llContainer = new LinearLayout(context);
llContainer.setOrientation(LinearLayout.VERTICAL);
llContainer.addView(hintTextView());
llContainer.addView(radioButtons());
return llContainer;
}
private TextView hintTextView() {
// TODO Auto-generated method stub
TextView tvHint = new TextView(context);
tvHint.setText(hint);
return tvHint;
}
private RadioGroup radioButtons() {
// TODO Auto-generated method stub
RadioGroup rbGroup = new RadioGroup(context);
rbGroup.setOrientation(RadioGroup.VERTICAL);
for (String value : choice_values){
RadioButton rbValue = new RadioButton(context);
rbGroup.addView(rbValue);
rbValue.setText(value);
}
return rbGroup;
}
}
This is how I create the control in my activity:
LinearLayout template_container = (LinearLayout) findViewById(R.id.llTemplate);
MultipleChoice mcControl = new MultipleChoice(getApplicationContext(), parts[0], choices);
control = mcControl.createRadioGroup();
template_container.addView(control);
I have tried something like this, but I'm not sure that I am trying the correct approach since it does not work:
View child = template_container.getChildAt(i);
LinearLayout v = ((LinearLayout)child);
View rgView = v.getChildAt(1);
RadioGroup rg = ((RadioGroup)rgView);
The RadioGroup
is added and displayed fine. All I want to do is get the value of the selected radio button. Thanks in advance!
EDIT
This is how I get the value of an EditText
and it works fine.
I get the control and add it to a List containing Views and then I do this with it to ghet the value if the view contains an EditText
:
String text = ((EditText)view).getText().toString().trim();