How do I retrieve data independently from nested JSON objects that are part of the same group in android/java? ( more clarity/details in body)

前端 未结 1 444
醉梦人生
醉梦人生 2021-01-17 03:26

I have a pretty common scenario, but I am stuck trying to figure out how to retrieve data from certain json objects within the same json response but returning different siz

相关标签:
1条回答
  • 2021-01-17 03:55

    Instead of trying to use response as it came from your API(which is causing problems), you could declare a model class that will be more friendly and perform some operations on your API response to create a list of your new model class.

    Declare a new model UpcomingGuest that takes a Visitor object and a Meeting in its constructor.

    public class UpcomingGuest {
    
    
        private String visitId;
    
        private String firstName;
    
        private String lastName;
    
        private String email;
    
        private Object phone;
    
        private String status;
    
        private String meetingId;
    
        private String meetingDateStart;
    
        private String meetingDateEnd;
    
        public UpcomingGuest(UpcomingGuestsList.Visitor visitor, UpcomingGuestsList.Meeting meeting){
    
            this.visitId = visitor.getVisitId();
            this.firstName = visitor.getFirstName();
            this.lastName = visitor.getLastName();
            this.email = visitor.getEmail();
            this.phone = visitor.getPhone();
            this.status = visitor.getStatus();
            this.meetingId = meeting.getId();
            this.meetingDateStart = meeting.getDateStart();
            this.meetingDateEnd = meeting.getDateEnd();
    
        }
    
        public String getVisitId() {
            return visitId;
        }
    
        public void setVisitId(String visitId) {
            this.visitId = visitId;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public Object getPhone() {
            return phone;
        }
    
        public void setPhone(Object phone) {
            this.phone = phone;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    
        public String getMeetingId() {
            return meetingId;
        }
    
        public void setMeetingId(String meetingId) {
            this.meetingId = meetingId;
        }
    
        public String getMeetingDateStart() {
            return meetingDateStart;
        }
    
        public void setMeetingDateStart(String meetingDateStart) {
            this.meetingDateStart = meetingDateStart;
        }
    
        public String getMeetingDateEnd() {
            return meetingDateEnd;
        }
    
        public void setMeetingDateEnd(String meetingDateEnd) {
            this.meetingDateEnd = meetingDateEnd;
        }
    
    
    }
    
    

    Then construct a list of UpcomingGuest from your response which will be used in your RecyclerView's adapter.

    .subscribe(response -> {
                        int sizercount = 0;
                        for (UpcomingGuestsList.Meeting meeting: response.meetings) {
                            for(UpcomingGuestsList.Visitor visitor: meeting.visitors){
                                upcomingGuests.add(new UpcomingGuest(visitor,meeting));
                            }
                        }
    
                        // send upcomingGuests results to view
    
    
                    }
    

    Then in your RecyclerView's adapter use a List of UpcomingGuest instead of the two Lists you are using.

    private List<UpcomingGuest> mUpcomingGuestList;
    

    EDIT

    For your sorting issue, you could sort your response before iterating through them like below:

    .subscribe(response -> {
            ArrayList<UpcomingGuest> upcomingGuests = new ArrayList<>();
    
            //sort meetings by date
            List<UpcomingGuestsList.Meeting> meetings = response.meetings;
            Collections.sort(meetings, new Comparator<UpcomingGuestsList.Meeting>() {
                @Override
                public int compare(UpcomingGuestsList.Meeting meeting, UpcomingGuestsList.Meeting t1) {
                    return meeting.getDateStart().compareTo(t1.getDateStart());
                }
            });
    
            for (UpcomingGuestsList.Meeting meeting: meetings) {
                //sort visitors by last name
                List<UpcomingGuestsList.Visitor> visitors = meeting.visitors;
                Collections.sort(visitors, new Comparator<UpcomingGuestsList.Visitor>() {
                    @Override
                    public int compare(UpcomingGuestsList.Visitor visitor, UpcomingGuestsList.Visitor t1) {
                        return visitor.getLastName().compareTo(t1.getLastName());
                    }
                });
    
                for(UpcomingGuestsList.Visitor visitor: visitors){
    
                    upcomingGuests.add(new UpcomingGuest(visitor,meeting));
                }
            }
    
            // send upcomingGuests results to view
    
    
    }
    
    0 讨论(0)
提交回复
热议问题