I'm new to Java/Android development. I am building a question/answers dynamically inside a viewflipper so that each flip has a new question with some answers. Right now, in my XML file I have a flipperview. The code below builds X number of [linearlayout with a [radiogroup and [4 radio elements]]]. My question is: how do I get the selected radio button based on the "current" visible window in the flipper?
for (DataQuizQuiz quiz_question : PLT.dataQuiz.getQuiz_data()) {
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
ll.setOrientation(LinearLayout.VERTICAL);
RadioGroup rg = new RadioGroup(this);
TextView tv_answer = new TextView(this);
tv_answer.setText("Question: " + quiz_question.getQuestion());
ll.addView(tv_answer);
for (DataQuizAnswers answer : quiz_question.getAnswers()) {
RadioButton rb = new RadioButton(this);
rb.setText(answer.getAnswer());
rg.addView(rb);
}
ll.addView(rg);
vf_quiz_data.addView(ll);
}
All I know is vf_quiz_data.getCurrentView()
, but beyond that I do not know how to reference the elements within that, since they do not have an id and are created on-the-fly. The code works to build the layout; I just am not sure how to reference the data inside it now. Thanks for any help.
Update: I came up with a way to target the radio group in the visible view but I think there has to be a better way. I assigned the radio group an id of a counter 0,1,2 etc. as it loops and capture the radio group element using this:
int selected = (int) ((RadioGroup)
vf_quiz_data.getCurrentView().findViewById(
vf_quiz_data.getDisplayedChild())).getCheckedRadioButtonId();
RadioButton b = (RadioButton) findViewById(selected);
Log.v("DEBUG",(String) b.getText());
I am also not sure how safe assigning ids based off of a counter is. If anyone has a alternate way of doing this please let me know.