I am trying to show the list of names (observation_name) from different objects in an array but I am just getting name from only the 1st index of the array its not iterating to the next name.
Here is the JSON:
{
"response_code": 1,
"status": "SUCCESS",
"message": "Record Found",
"result": {
"observations": [
{
"id": 1,
"observation_name": "Test Observation",
"visit_id": 2,
"created_at": "2022-03-12T07:55:57.000000Z",
"updated_at": "2022-03-12T07:55:57.000000Z"
},
{
"id": 2,
"observation_name": "Test Observation 2",
"visit_id": 2,
"created_at": "2022-03-12T07:55:57.000000Z",
"updated_at": "2022-03-12T07:55:57.000000Z"
},
{
"id": 3,
"observation_name": "Test Observation",
"visit_id": 2,
"created_at": "2022-03-12T07:56:18.000000Z",
"updated_at": "2022-03-12T07:56:18.000000Z"
},
{
"id": 4,
"observation_name": "Test Observation 2",
"visit_id": 2,
"created_at": "2022-03-12T07:56:18.000000Z",
"updated_at": "2022-03-12T07:56:18.000000Z"
},
{
"id": 5,
"observation_name": "Test Observation",
"visit_id": 2,
"created_at": "2022-03-13T09:56:09.000000Z",
"updated_at": "2022-03-13T09:56:09.000000Z"
},
{
"id": 6,
"observation_name": "Test Observation 2",
"visit_id": 2,
"created_at": "2022-03-13T09:56:09.000000Z",
"updated_at": "2022-03-13T09:56:09.000000Z"
},
{
"id": 7,
"observation_name": "Test Observation",
"visit_id": 2,
"created_at": "2022-03-13T09:56:58.000000Z",
"updated_at": "2022-03-13T09:56:58.000000Z"
},
{
"id": 8,
"observation_name": "Test Observation 2",
"visit_id": 2,
"created_at": "2022-03-13T09:56:58.000000Z",
"updated_at": "2022-03-13T09:56:58.000000Z"
},
{
"id": 9,
"observation_name": "Test Observation 3",
"visit_id": 2,
"created_at": "2022-03-13T09:56:58.000000Z",
"updated_at": "2022-03-13T09:56:58.000000Z"
}
]}}
This is how I am parsing the data:
try {
if (apiCalled == APICodes.GET_VISIT_DETAILS) {
if (jsonResponse.getInt("response_code") == 1) {
JSONObject result = jsonResponse.getJSONObject("result");
JSONArray observations = result.getJSONArray("observations");
PrescriptionModel model = new PrescriptionModel();
for (int i = 0; i < observations.length(); i++) {
JSONObject observationItem = observations.getJSONObject(i);
Log.e("Observations:", observationItem.getString("observation_name"));
model.setObservation_name(observationItem.getString("observation_name"));
observationsList.add(model);
adapter.notifyDataSetChanged();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Looking for help, As I am kinda new to JSON parsing. Any help will be appreciated
>Solution :
It looks like you are always overwriting the name of PrecriptionModel. Instead, you probably want to create a new PrecriptionModel for each array entry. Also, I think the notifyDataSetChanged() is only needed after you added all new items to the list. So, try this please:
if (jsonResponse.getInt("response_code") == 1) {
JSONObject result = jsonResponse.getJSONObject("result");
JSONArray observations = result.getJSONArray("observations");
for (int i = 0; i < observations.length(); i++) {
PrescriptionModel model = new PrescriptionModel();
JSONObject observationItem = observations.getJSONObject(i);
Log.e("Observations:", observationItem.getString("observation_name"));
model.setObservation_name(observationItem.getString("observation_name"));
observationsList.add(model);
}
adapter.notifyDataSetChanged();
}