3

I want to create radio button,checkbox,spinner dynamically from web service. Below is my response.

{
    "getSurvey": {
        "ErrorCode": "0",
        "Result": "Success",
        "Response": [
            {
                "SurveyId": "91",
                "SurveyName": "Sample Survey",
                "Questions": [
                    {
                        "QuestionId": "553",
                        "QuestionText": "<p style=\"margin: 0px; text-align: left;\">Are you satisfied with Cement Quality?</p>",
                        "Type": "RadioButtonList",
                        "Mandatory": "Y",
                        "OptionValue": [
                            {
                                "AnswerId": "2220",
                                "AnswerValue": "Excellent"
                            },
                            {
                                "AnswerId": "2221",
                                "AnswerValue": "Good"
                            },
                            {
                                "AnswerId": "2222",
                                "AnswerValue": "Satisfactory"
                            },
                            {
                                "AnswerId": "2223",
                                "AnswerValue": "Poor"
                            }
                        ]
                    },
                    {
                        "QuestionId": "554",
                        "QuestionText": "How will you rate Cement? (0= Lowest and 7= Highest)&nbsp;",
                        "Type": "DropDownList",
                        "Mandatory": "Y",
                        "OptionValue": [
                            {
                                "AnswerId": "2224",
                                "AnswerValue": "0"
                            },
                            {
                                "AnswerId": "2225",
                                "AnswerValue": "1"
                            },
                            {
                                "AnswerId": "2226",
                                "AnswerValue": "2"
                            },
                            {
                                "AnswerId": "2227",
                                "AnswerValue": "3"
                            },
                            {
                                "AnswerId": "2228",
                                "AnswerValue": "4"
                            },
                            {
                                "AnswerId": "2229",
                                "AnswerValue": "5"
                            },
                            {
                                "AnswerId": "2230",
                                "AnswerValue": "6"
                            },
                            {
                                "AnswerId": "2231",
                                "AnswerValue": "7"
                            }
                        ]
                    },
                    {
                        "QuestionId": "555",
                        "QuestionText": "What Kind of issues you face while Delivery?",
                        "Type": "ListBox",
                        "Mandatory": "Y",
                        "OptionValue": [
                            {
                                "AnswerId": "2232",
                                "AnswerValue": "Logistic Issue"
                            },
                            {
                                "AnswerId": "2233",
                                "AnswerValue": "Material Handeling "
                            },
                            {
                                "AnswerId": "2234",
                                "AnswerValue": "Stock not available"
                            },
                            {
                                "AnswerId": "2235",
                                "AnswerValue": "Wastage of Cement"
                            },
                            {
                                "AnswerId": "2236",
                                "AnswerValue": "Others"
                            }
                        ]
                    },
                    {
                        "QuestionId": "559",
                        "QuestionText": "Do you like Cement?",
                        "Type": "CheckBoxList",
                        "Mandatory": "Y",
                        "OptionValue": [
                            {
                                "AnswerId": "2237",
                                "AnswerValue": "Yes"
                            },
                            {
                                "AnswerId": "2238",
                                "AnswerValue": "No"
                            }
                        ]
                    }
                ]
            }
        ]
    }
} 

Based on type i need to create views dynamically. Please someone give me the idea or solution how to achieve this. After created views dynamically i need to get all the selected values from created views.

Thanks.

Shanmuga M
  • 115
  • 2
  • 7

5 Answers5

7

you can use json2view library to create dynamically view.

https://github.com/Avocarrot/json2view

Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
4

This is tricky, you need to learn how to create layouts dynamically in code first. I would start off my reviewing tutorials found here: http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/

You would need to create a class that could parse this data and create the views specifically for your values. Once that is done, you could read the data again or have it stored so you could load the data to the screen. This could also be done at the same time you are creating the layout.

Brandon Wilson
  • 4,462
  • 7
  • 60
  • 90
  • Thanks for your answer..Can you please tell me how to place corresponding QuestionText below that corresponding OptionValue for dynamic view in linear layout. – Shanmuga M May 28 '15 at 03:21
  • This helped me when I started using `JSON` in my project http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android There are several websites that walk you through parsing `JSON` objects. This really depends on what the structure of the `JSON` is and how it is laid out. I would go here http://jsonviewer.stack.hu/ and put you `JSON` in and see what is an `Array` and what is an `Object`, the go from there. – Brandon Wilson May 28 '15 at 03:26
  • I successfully parsed that response and stored in array list. But i don't how to create view for all the questions with corresponding option. – Shanmuga M May 28 '15 at 03:30
  • 1
    The Dreamincode.net link above should get you goin. Basically, you will need to create a class to handle and process the data. You can create functions that take in the data to use. Based on the data you receive in, you can create you different buttons, check boxes, and so on. Then you will add what you have just created to your view. I would recommend looking over that website and go from there. You will use `.addView()` in Android to add to the few and `this.setContentView()` to set the view once everything is added or updated. – Brandon Wilson May 28 '15 at 03:36
0

If the type of Questions like '"Type": "RadioButtonList"' are known in advance, you can create predefined layouts addressing each of those types. You then need to parse and look for the object /Response/Questions/Type in your json and inflate the appropriate layout. You also need to create an adapter that can accept list of options for each type. Plugin the adapter and the view behind the layout.

I do something similar for the product I work on. After I retrieve the json feed, I would scan through the Questions to see the type of layouts I require. In my scenario, I create a ViewPager dynamically: each entry into the view pager would have a layout corresponding to the Type of Question. I would then build up the list of options for each Question Type and feed them to an adapter that is bound to listview or drop down list or a custom type that resides in the layout file.

Vino
  • 1,544
  • 1
  • 18
  • 26
0

just parse and set data to static data in code

ScrollView sv = new ScrollView(this);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);


    //radio list
    RadioGroup group = new RadioGroup(this); 
    group.setOrientation(RadioGroup.VERTICAL);

    RadioButton btn1 = new RadioButton(this);
    btn1.setText("BTN1");
    group.addView(btn1);

    RadioButton btn2 = new RadioButton(this);
    btn2.setText("BTN2");
    group.addView(btn2);

    ll.addView(group);


    //drop down
    Spinner spiner = new Spinner(this);
    List<String> data = new ArrayList<String>();
    data.add("Spinner - one");
    data.add("Spinner - two");
    data.add("Spinner - three");
    data.add("Spinner - four");
    data.add("Spinner - five");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
    spiner.setAdapter(adapter);

    ll.addView(spiner);

    //listview
    ListView listData = new ListView(this);
    List<String> dataList = new ArrayList<String>();
    dataList.add("List - one");
    dataList.add("List - two");
    dataList.add("List - three");
    dataList.add("List - four");
    dataList.add("List - five");
    dataList.add("List - six");
    dataList.add("List - seven");
    dataList.add("List - eight");
    dataList.add("List - nine");
    dataList.add("List - ten");
    dataList.add("List - eleven");
    dataList.add("List - twelve");
    dataList.add("List - thirteen");
    dataList.add("List - fourteen");
    dataList.add("List - fifteen");
    ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,    
    android.R.layout.simple_list_item_1, dataList);
    listData.setAdapter(listAdapter);

    ll.addView(listData);
    this.setContentView(sv);

    setListViewHeightBasedOnChildren(listData);

support method

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
krunal shah
  • 364
  • 2
  • 14
0

Try this. Hope it might help you.

public class MainActivity extends Activity {
 String response = "";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  try {
   JSONObject jObject1 = new JSONObject(response);
   JSONObject jObject2 = jObject1.getJSONObject("getSurvey");
   if (jObject2.getString("ErrorCode").equals("0")) {
    JSONArray jArray = jObject2.getJSONArray("Response");
    JSONObject jObject3 = jArray.getJSONObject(0);// Response array
                // contains only
                // one object ,
                // otherwise we
                // can put loop
    JSONArray jArray2 = jObject3.getJSONArray("Questions");
    for (int i = 0; i < jArray2.length(); i++) {

     JSONObject jObjectQ = jArray2.getJSONObject(i);
     String QUESTION = jObjectQ.getString("QuestionText"); // Similarly
                   // you
                   // can
                   // take
                   // other
                   // parameters.
     JSONArray jArrayA = jObjectQ.getJSONArray("OptionValue");
     for (int j = 0; j < jArrayA.length(); j++) {
      JSONObject jObjectOPTION = jArrayA.getJSONObject(j);
      String ANSWERID = jObjectOPTION.getString("AnswerId");
      String ANSWERVALUE = jObjectOPTION
        .getString("AnswerValue");

      // Add Dynamically in Spinner or create Radiobutton as
      // per your requirement
     }

    }

   }

  } catch (JSONException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
}

You can also set Id dynamically of each controls. based on that you should call any method and check id of the controls.

Arth Tilva
  • 2,496
  • 22
  • 40