2013-11-01 75 views
0

我需要實現一個選擇題。我必須添加單選按鈕和文本視圖以供多項選擇。我需要根據選擇的數量動態地實現它。有人可以幫助我無線電組和單選按鈕

for (Answer answer : answers) { 
    LinearLayout linearLayoutRw2 = new LinearLayout(this); 
    linearLayoutRw2 
      .setLayoutParams(new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT, 
        LayoutParams.WRAP_CONTENT)); 

    RadioGroup radioGroup = new RadioGroup(
      ShowQuestionsActivity.this); 
    linearLayoutRw2.addView(radioGroup); 

    RadioButton rb = new RadioButton(ShowQuestionsActivity.this); 
    radioGroup.addView(rb); 

    TextView ansText = new TextView(ShowQuestionsActivity.this); 
    ansText.setText(Html.fromHtml(answer.getAnswerText())); 
    linearLayoutRw2.addView(ansText); 

    linearLayoutShowQues.addView(linearLayoutRw2); 

} 

回答

0

嘗試這樣,

首先創建一個動態ListView

ListView choicelist = new ListView(this); 
    choicelist.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    choicelist.setAdapter(new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_single_choice, 
        android.R.id.text1, your_answer_list)); 
    choicelist.setSelector(new ColorDrawable(0x0)); 
    choicelist.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
    choicelist.setCacheColorHint(0); 
    choicelist.setVerticalFadingEdgeEnabled(false); 

最後名單添加到您的佈局

linearLayoutShowQues.addView(choicelist); 

定製,你可以使用自定義適配器。

希望這會幫助你。

+0

我解決了這個問題,for循環和無線電roup .... – ammu

+0

List answers = question.getAnswers(); \t \t \t如果(答案= NULL && answers.size()> 0!){ \t \t \t \t RadioGroup中radioGroup中=新RadioGroup中( \t \t \t \t \t \t MNActivity.this); \t \t \t \t int num = answers.size(); \t \t \t \t對(INT I = 0;我 ammu