JSON Response:
{"schedulePlan":[{"slot_id":1,"slot":"08:00 AM - 09:00 AM","slotStatus":"booked","booking_details":[{"booking_id":"33","patientName":"Malik","patientAddress":"D-77, Sector 63, Noida"}]},{"slot_id":2,"slot":"09:00 AM - 10:00 AM","slotStatus":"available","booking_details":[{"booking_id":"","patientName":"","patientAddress":""}]},{"slot_id":3,"slot":"10:00 AM - 11:00 AM","slotStatus":"booked","booking_details":[{"booking_id":"22","patientName":"Malik","patientAddress":"D-77, Sector 63, Noida"},{"booking_id":55,"patientName":"Om","patientAddress":"Sector 33, Noida"}]},{"slot_id":4,"slot":"11:00 AM - 12:00 PM","slotStatus":"available","booking_details":[{"booking_id":"","patientName":"","patientAddress":""}]},{"slot_id":5,"slot":"12:00 PM - 01:00 PM","slotStatus":"booked","booking_details":[{"booking_id":"76","patientName":"Raj","patientAddress":"Sector 83, Noida"}]}]}
Output Required:
Patient Name: Malik
Patient Name:
Patient Name: Malik, Om
Patient Name:
Patient Name: Raj
And same for address as well.
On every list item click, need to display patient name. How to parse the response and display patientName and patientAddress as above?
Models: Model 1:
public class RescheduleModel {
private String slot_id;
private String slot;
private String slotStatus;
public ArrayList<BookingDetailsModel> bookingDetailsModel = new ArrayList<BookingDetailsModel>();
public String getSlot_id() {
return slot_id;
}
public void setSlot_id(String slot_id) {
this.slot_id = slot_id;
}
public String getSlot() {
return slot;
}
public void setSlot(String availableSlot) {
this.slot = availableSlot;
}
public String getSlotStatus() {
return slotStatus;
}
public void setSlotStatus(String slotStatus) {
this.slotStatus = slotStatus;
}
public List<BookingDetailsModel> getBookingDetailsModels() {
return bookingDetailsModel;
}
public void setBookingDetailsModel(ArrayList<BookingDetailsModel> bookingDetailsModel) {
this.bookingDetailsModel = bookingDetailsModel;
}
Model 2:
public class BookingDetailsModel {
private String booking_id;
private String patientName;
private String patientAddress;
public String getBooking_id() {
return booking_id;
}
public void setBooking_id(String booking_id) {
this.booking_id = booking_id;
}
public String getPatientName() {
return patientName;
}
public void setPatientName(String patientName) {
this.patientName = patientName;
}
public String getPatientAddress() {
return patientAddress;
}
public void setPatientAddress(String patientAddress) {
this.patientAddress = patientAddress;
}
Activity:
public void parseJsonResponse(String result) {
Log.i(TAG, result);
try {
JSONObject json = new JSONObject(result);
JSONArray jArray = new JSONArray(json.getString("schedulePlan"));
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
RescheduleModel rescheduleModel = new RescheduleModel();
rescheduleModel.setSlot_id(jObject.getString("slot_id"));
rescheduleModel.setSlot(jObject.getString("slot"));
rescheduleModel.setSlotStatus(jObject.getString("slotStatus"));
JSONArray array = jObject.getJSONArray("booking_details");
for (int j = 0; j < array.length(); j++) {
JSONObject object = array.getJSONObject(j);
BookingDetailsModel model = new BookingDetailsModel();
model.setPatientName(object.getString("patientName"));
model.setPatientAddress(object.getString("patientAddress"));
Log.i("PNAME...", object.getString("patientName"));
Log.i("PADDRESS...", object.getString("patientAddress"));
bookingDetails.add(model);
}
slots.add(rescheduleModel);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
Output:
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Malik
04-21 15:17:04.431 28588-28588/com.example I/PADDRESS...: D-77, Sector 63,
Noida
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Malik
04-21 15:17:04.431 28588-28588/com.example I/PADDRESS...: D-77, Sector 63,
Noida
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Om
04-21 15:17:04.431 28588-28588/com.example I/PADDRESS...: Sector 33, Noida
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Raj
04-21 15:17:04.432 28588-28588/com.example I/PADDRESS...: Sector 83, Noida
I want to display "Malik, Om" when i click third row in the listview. Currently Malik and Om are getting added to different rows.
OnClick:
nextDetail.setText("Next slot is booked for '" + bookingDetails.get(position + 1).getPatientName() + "' at '" +
bookingDetails.get(position + 1).getPatientAddress() + "'.");