user8601021
user8601021

Reputation: 219

populating arraylist of arraylist

I have an arraylist of arraylist which I am tring to populate but its not working.The response is being fetched from server.the response comes as follows

[{"QKey":"1234","OptionLabel":"Ground Floor","optionValue":"0"},{"QKey":"5678","OptionLabel":"1st Floor","optionValue":"1"}

I am trying to fetch it,add it in arraylist and populate but it seems to be not working

this is my code

String dropDownResponse=readFromFile(2);
                                             Log.d("Reading from file",dropDownResponse);
                                             JSONArray jsonArray = new JSONArray(dropDownResponse);

                                             formModel.setName(rowLabel);
                                             formModel.setIsMandatory(isMandatory);
                                             formModel.setInputType(inputType);
                                            /* formModel.setName("SAMPLE LABEL");
                                             formModel.setIsMandatory("Y");
                                             formModel.setInputType("selectbox");*/
                                             spinnerList.add(formModel);
                                             spinnerPopulationList.get(spinnerList.size()-1).set(0,rowLabel);
                                             for(int j=0;j<jsonArray.length();j++)
                                             {
                                                 JSONObject jsonObject = jsonArray.getJSONObject(j);
                                                 spinnerRowId=jsonObject.getString("QKey");
                                                 Log.d("QKey",spinnerRowId);
                                                 optionLabel=jsonObject.getString("OptionLabel");
                                                 Log.d("Option Label",optionLabel);
                                                 if(rowId.equals(spinnerRowId))
                                                 {
                                                     spinnerPopulationList.get(spinnerList.size()-1).set(spinnerPopulationList.get(spinnerList.size()-1).size()-1,optionLabel);
                                                 }
                                             }
                                             for(int h=0;h<spinnerPopulationList.get(spinnerList.size()-1).size();h++)
                                             {
                                                 Log.d("spinner item"+rowLabel+"["+h+"]",spinnerPopulationList.get(spinnerList.size()-1).get(h));
                                             }

this line in the code shows indexOutOfBoundException

if(rowId.equals(spinnerRowId))
                                                 {
                                                     spinnerPopulationList.get(spinnerList.size()-1).set(spinnerPopulationList.get(spinnerList.size()-1).size()-1,optionLabel);
                                                 }

Upvotes: 0

Views: 67

Answers (1)

Kashan Danish
Kashan Danish

Reputation: 155

I don't think you need a two dimensional AllayList to accomodate this json. This is just an array of objects. You can use Gson to parse it quite easily.

You will need a couple of response classes like

class ResponseObj {
    private String Qkey;
    private String OptionLabel;
    private String optionValue;

    //Constructor(s), getters and setters
}

class Response {
    private ArrayList<ResponseObj> objects = new ArrayList<>();

    //Constructor(s), getters and setters
}

Then you can use Gson to parse the json and make an object out of it. You can use something like this where you are getting the response from server.

Response response = gson.fromJson(YOUR_JSON, Response.class);

for(ResponseObj object : response.getObjects()) {

    //In this loop, you are iterating over each object in your json
    //which looks like
    //{"QKey":"1234","OptionLabel":"Ground Floor","optionValue":"0"}

    doSomething(object);
    doSomethingWithKey(object.getQKey());
}

Here is how you can use Gson in your project.

Upvotes: 1

Related Questions