0

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);
                                                 }
2
  • what seems not working..while fetching or populate ..adding where? Commented Mar 7, 2018 at 9:06
  • I have the response and earlier the data from string QKey and Optionlabel was getting fetched but now that I want to add it to arraylist of arraylist arraylist to populate it within dynamic spinners I cannot even see that fetched data Commented Mar 7, 2018 at 9:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I need a two dimensional array becuase I have to filter the response according to its nature and then populate in dynamic spinner.each spinner will be populated with a different arraylist.so the two dimensional array is to first store the arraylist index and then store the items
Well then there may be some problem with your response. The json you posted with your answer, it can only be used to populate one ArrayList. Maybe you haven't posted full json?
I have not posted the complete json.I just posted the structure.I need to add it to spinnerPopulationList which is ArrayList<ArrayList<String>> type.
Lets say you are getting just the part of json which you have posted here. How will you final ArrayList<ArrayList<String>> look like using that json? I am still trying to understand the objective here.
there will be a number of lists to be populated.say 3 lists in arraylists and then populating the 3 lists in 3 dynamic spinners
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.