获取动态创建的所有选定单选按钮和复选框值?

时间:2014-04-28 13:29:20

标签: android android-edittext textview radio-button checkboxlist

我想保存所有选中的答案(checkbox和radiobutton)及其在sqlite db中动态Textview中设置的相应问题,              但问题是当我点击"保存按钮"只有最后选择     单选按钮值保存为sqlite,不是所有选中的值,也不能获得所有选中的复选框值              请帮帮我。

save= (Button) findViewById(R.id.save);
        save.setOnClickListener(this);
        ll = (LinearLayout) findViewById(R.id.linearLayout1);

        for (int j = 0; j < Questions.length; j++) {
               tv = new TextView(this);
               tv.setId(j);
            tv.setText(Questions[j].getQuestionNo() + "."
                    + Questions[j].getQuestion());
            ll.addView(tv);

            Answer[] answer = Questions[j].getAnswer();
            if (Questions[j].getMultipleChoice().equalsIgnoreCase("false")) {
                rg = new RadioGroup(this);
                rg.setOrientation(RadioGroup.VERTICAL);
                RadioButton[] rb = new RadioButton[answer.length];
                for (int i = 0; i < answer.length; i++)
                {
                     // add radio buttons
                    rb[i] = new RadioButton(this);
                    rb[i].setText(answer[i].getLabel());
                    int id = Integer.parseInt(j + "" + i);
                    rb[i].setId(id);
                    rg.addView(rb[i]);
                            }
                ll.addView(rg);

            else 
                        {
                            // add checkboxes
                   for (int i = 0; i < answer.length; i++) {
                    cb = new CheckBox(this);
                    cb.setText(answer[i].getLabel());
                    int id = Integer.parseInt(j + "" + i);
                    cb.setId(id);

                    ll.addView(cb);
                                                            } 
                        } 


}



 public void onClick(View v) {
            switch (v.getId()) {
            case R.id.save:

                if (rg.getCheckedRadioButtonId() != -1) {
                    int id = rg.getCheckedRadioButtonId();
                    View radioButton = rg.findViewById(id);
                    int radioId = rg.indexOfChild(radioButton);
                    RadioButton btn = (RadioButton) rg.getChildAt(radioId);
                    String answer = (String) btn.getText();
                    //only last selected radio button value is coming 
                             }

                         // not able to get all the selected checkbox value
                break;

            default:
                break;
            }
        }

1 个答案:

答案 0 :(得分:0)

我不知道android.But问题出现在struts2.I我通过生成如下的动态复选框解决了这个问题。

<s:iterator value="samplelist">
<input type="checkbox"  value="true" style="margin-top:2px;" name="p_<s:property value='profileId'/>
</s:iterator>

所以我通过使用for循环

找出了它是否在java代码中被检查过
for(int i=0;i<samplelist.size();i++)
request.getParameter("P_something");