How to group api response in android

一个人想着一个人 提交于 2021-02-11 06:13:13

问题


This is the json response I am recieving:

[
    {
        "id": "1",
        "sports": "Cricket",
        "name": "Suraj" 
        "year": "2017",
        "rank": "1",
        "age": "25",
    },
    {
        "id": "2",
        "sports": "Cricket",
        "name": "Rajeev"
        "year": "2017",
        "rank": "2",
        "age": "24",
    },
    {
        "id": "3",
        "sports": "Cricket",
        "name": "Kiran"
        "year": "2015",
        "rank": "1",
        "age": "25",
    },
    {
        "id": "4",
        "sports": "Badminton"
        "name": "Shekhar"
        "year": "2017",
        "rank": "1",
        "age": "22",
    },
    {
       "id": "5",
        "sports": "Badminton"
        "year": "2017",
        "name": "Karan"
        "rank": "2",
        "age": "22",
    },
    {
        "id": "6",
        "sports": "Cricket",
        "name": "Ali"
        "year": "2017",
        "rank": "3",
        "age": "26",
    }
]

I need to represent this information in a table after grouping it according to the sports type, then again sub group it into years, as in the picture I have attached. But I have no idea how to do that. Can someone help me with this ?

I have created this model class:

public class prev_data {

      private String id;
    
        private String sports;
    
        private String name;
    
        private String year;
    
        private String rank;
    
        private String age;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getSports() {
            return sports;
        }
    
        public void setSports(String sports) {
            this.sports = sports;
        }
....

}

And this is how I am calling the api through retrofit:

List<prev_data> prevList;

 Call<List<prev_data>> call3 = apiInterface.getPrev_data();
        call3.enqueue(new Callback<List<prev_data>>() {

            @Override
            public void onResponse(Call<List<prev_data>> call3, Response<List<prev_data>> response3) {
               prevList= response3.body();
            }

            @Override
            public void onFailure(Call<List<prev_data>> call3, Throwable t) {
            }
        });

Any help will be highly appreciated and acknowledged.

来源:https://stackoverflow.com/questions/64252837/how-to-group-api-response-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!