Get all the selected radio button and check box value created dyamically?

拜拜、爱过 提交于 2019-12-12 10:26:43

问题


I want to save all the selected answer(checkbox and radiobutton) and its corresponding question which is set in dynamic Textview in sqlite db , but the problem is when i am clicking "save button" only last selected radio button value is saved to sqlite , not all selected value and not able to get all selected check box value also .
Please help me .

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:


I don't know android.But the issue arise me in struts2.I solved that issue by generating dynamic checkbox like below.

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

so i have found out if it is checked or not in java code by using the for loop

for(int i=0;i<samplelist.size();i++)
request.getParameter("P_something");


来源:https://stackoverflow.com/questions/23342432/get-all-the-selected-radio-button-and-check-box-value-created-dyamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!