-2

I want to set a nubmer of meetings per day in a recycler view. I am getting JSON object as a Main object which consist of array named data data consist of object which has date along with it it consist of a array which have n number of objects. My question is how to bound date in data array with time in my secondary array.

I had tried to fetch data using volley i am getting the data but whats happening is the last object in time is displaying number of times which is equal to number of object present in time array.

 dataModelArrayList = new ArrayList<>();

   JSONArray dataArray = obj.getJSONArray("data"); 
   for (int i = 0; i < dataArray.length(); i++) {                                    
        playerModel = new VipPojo();              
        JSONObject dataobj = dataArray.getJSONObject(i);
        playerModel.setDate(dataobj.getString("date"));                                    
        JSONArray dataArrays1 = dataobj.getJSONArray("time");                                    
        for (int j = 0; j < dataArrays1.length(); j++) {
            JSONObject dataobj1 = dataArrays1.getJSONObject(j);                                            
            playerModel.setTimes(dataobj1.getString("times"));
            playerModel.setNotice(dataobj1.getString("notice"));
            playerModel.setCategory(dataobj1.getString("category"));
            dataModelArrayList.add(playerModel)
        }
    }
    rvAdapter = new MeetingAdapter(getActivity().getApplicationContext(), dataModelArrayList); 
    recyclerView.setAdapter(rvAdapter);
    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),, getResources().getInteger(R.integer.number_of_grid_items));

This is my adapter onBindViewHolder

holder.empname.setText(dataModelArrayList.get(position).getDate());
holder.postname.setText(dataModelArrayList.get(position).getTimes());

Here is the JSON

{
    "data": [
        {
            "date": "Thursday 1, August, 2019",
            "time": [
                {
                    "times": "1:13PM to 1:13PM",
                    "notice": "testing",
                    "category": "Meeting"
                },
                {
                    "times": "12:00PM to 1:00PM",
                    "notice": "Meeting",
                    "category": "Meeting"
                }
            ]
        },
        {
            "date": "Friday 2, August, 2019",
            "time": [
                {
                    "times": "3:00PM to 3:30PM",
                    "notice": "Appointment",
                    "category": "Meeting"
                },
                {
                    "times": "12:00PM to 12:30PM",
                    "notice": "Appointment",
                    "category": "Meeting"
                }
            ]
        },
        {
            "date": "Monday 5, August, 2019",
            "time": [
                {
                    "times": "11:00AM to 11:30AM",
                    "notice": "Obj",
                    "category": "Meeting"
                }
            ]
        }
    ],
    "responseCode": 1,
    "responseMessage": ""
}

Expecting to link data with correct time.

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
lakshman kumar
  • 341
  • 4
  • 14
  • Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Rohit5k2 Aug 06 '19 at 08:40

1 Answers1

0

Your code walkthrough:

VipPojo playerModel;
try {
    JSONArray dataArray = obj.getJSONArray("data");
    for (int i = 0; i < dataArray.length(); i++) {
        // playerModel = new VipPojo();
        JSONObject dataobj = dataArray.getJSONObject(i);
        JSONArray dataArrays1 = dataobj.getJSONArray("time");
        for (int j = 0; j < dataArrays1.length(); j++) {
            playerModel = new VipPojo(); // initialize here
            JSONObject dataobj1 = dataArrays1.getJSONObject(j);
            playerModel.setDate(dataobj.getString("date"));
            playerModel.setTimes(dataobj1.getString("times"));
            playerModel.setNotice(dataobj1.getString("notice"));
            playerModel.setCategory(dataobj1.getString("category"));
            dataModelArrayList.add(playerModel);
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
}

but your "date" object is adding previously assigned value to your data model since you are using setDate() inside the inner loop which you don't that. that's a little above my paygrade, but until you find a solution you can move "date" object inside time array next to notice, category,... this will solve it for now.

S.R
  • 2,819
  • 2
  • 22
  • 49
  • in log i am getting correct values but my question how to set values in adapter? – lakshman kumar Aug 09 '19 at 09:34
  • @lakshmankumar you should initialize your data object inside the inner loop. see updated answer – S.R Aug 09 '19 at 13:33
  • I had initialized data object inside inner loop but whats happening is if i have two objects in time array then in my recycler view i am getting second object in the array printed twice in my view where first object in that array is not displaying. – lakshman kumar Aug 14 '19 at 06:23