Brownman Revival Brownman Revival - 1 month ago 8
Android Question

radio group not working and vertical alignment android

JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
LinearLayout lll = new LinearLayout(Questionnaire.this);
lll.setOrientation(LinearLayout.VERTICAL);


JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
System.out.println(obj2.get("question"));

TextView tv = new TextView(Questionnaire.this);

tv.setText(obj2.get("question").toString());
lll.addView(tv);




JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
Log.d("Reading Answer: ", jsonAnswer + "");

for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
TextView tv1 = new TextView(Questionnaire.this);
tv1.setText(obj3.get("inputname").toString());


RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL

if (obj3.get("inputtype").toString().matches("radio")) {

RadioButton newRadioButton = new RadioButton(Questionnaire.this);
newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
newRadioButton.setText(obj3.get("inputname").toString());
group.addView(newRadioButton);


lll.addView(group);

}







}

}
ll.addView(lll);

}


This is my code for dynamically adding radio button.

Problem is when I select one option then select anther it does not deselect the previous one. Why doesn't it deselect the the first one selecting another when they are both added to same radio group. Also it is not placing the radio buttons next to each other but on top of each other when the orientation is set to horizontal

UPDATE

I moved my declaration of

RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL


out side parent for loop now i am getting


java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


How should i deal with it?

Answer

you are added only one RadioButton in RadioGroup that's why you got many selected radio button. RadioGroup need minimum 2 RadioButton for check or Uncheck effect.

try this,I hope it may work.

JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());

for (int iii = 0; iii < jsonQuestion.length(); iii++) {
    LinearLayout lll = new LinearLayout(Questionnaire.this);
    lll.setOrientation(LinearLayout.VERTICAL);

    JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
    System.out.println(obj2.get("question"));

    TextView tv = new TextView(Questionnaire.this);
    tv.setText(obj2.get("question").toString());
    lll.addView(tv);

    JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
    Log.d("Reading Answer: ", jsonAnswer + "");

    RadioGroup group = new RadioGroup(Questionnaire.this);
    group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL

   for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
        JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
        System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
        if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
            TextView tv1 = new TextView(Questionnaire.this);
            tv1.setText(obj3.get("inputname").toString());

            if (obj3.get("inputtype").toString().matches("radio")) {
                RadioButton newRadioButton = new RadioButton(Questionnaire.this);
                newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
                newRadioButton.setText(obj3.get("inputname").toString());
                group.addView(newRadioButton);
            }
        }
    }
    lll.addView(group);
    ll.addView(lll);
}
Comments