我是Java/Android開發新手。我在viewflipper中動態構建一個問題/答案,以便每個翻頁都有一個帶有答案的新問題。現在,在我的XML文件中,我有一個flipperview。下面的代碼用[radiogroup和[4 radio elements]]]建立了X個[linearlayout]。我的問題是:如何獲得基於鰭狀肢「當前」可見窗口的選定單選按鈕?使用Java Android,有沒有辦法在flipperview中訪問動態對象?
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);
}
我所知道的是vf_quiz_data.getCurrentView()
,但除此之外,我不知道如何引用內的元素,因爲他們沒有一個id和即時創建。代碼用於構建佈局;我只是不確定如何引用它裏面的數據。謝謝你的幫助。
更新: 我想出了一種在可見視圖中定位無線電組的方法,但我認爲必須有更好的方法。我分配無線電集團的計數器0,1,2等的標識,因爲它循環和使用這種捕捉無線電集團元素:
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());
我也不知道如何基於安全關計數器將ID分配是。如果有人有這樣做的替代方式,請讓我知道。