2012-05-09 37 views
0

我是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分配是。如果有人有這樣做的替代方式,請讓我知道。

回答

0

一位朋友告訴我爲我的數據構建自定義視圖或視圖,以便可以使用vf_quiz_data.getCurrentView()從我自己的方法訪問它。經過一系列試驗和錯誤之後,我得到了一個測試示例。我的自定義類叫做「ViewQuiz」,我添加了一個名爲「getName()」的方法,它返回了類添加到視圖中的edittext的值。我最終能夠像這樣檢索它:(ViewQuiz) vf_quiz_data.getCurrentView()).getName()。我的課程擴展了線性佈局,並在裏面放置了一個edittext,我在一個循環中創建了一個新的類實例,並將它添加到了viewflipper中。櫃面這是對別人有幫助這裏是例子:

public class ViewQuiz extends LinearLayout { 

public EditText name; 

public ViewQuiz(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    name = new EditText(context); 

    name.setText("This is a test"); 
    addView(name); 
} 

public ViewQuiz(Context context) { 
    super(context); 
    name = new EditText(context); 
    name.setText("This is a test"); 
    addView(name); 
} 

public String getName() { 
    return name.getText().toString(); 

} 

} 

ViewQuiz test; 
for (loop stuff here) { 
     test = new ViewQuiz(this); 
     test.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     // vf_quiz_data = (ViewFlipper) findViewById(R.id.vf_quiz_data); 
     vf_quiz_data.addView(test); 
} 

// get text from visible view 
(ViewQuiz) vf_quiz_data.getCurrentView()).getName() 
相關問題