简体   繁体   中英

How To set Json data in RecylerView in Hetrogeneous form of data in android studio

This json data to set like this RecyelView list in android studio

[
    {
        "date": "03-01-2018",
        "price": 60.86,
        "company": "Bharat Petroleum",
        "city": "Panaji",
        "type": "Diesel"
    },
    {
        "date": "03-01-2018",
        "price": 64.49,
        "company": "Bharat Petroleum",
        "city": "Panaji",
        "type": "Petrol"
    },
    {
        "date": "03-01-2018",
        "price": 60.81,
        "company": "Indian Oil",
        "city": "Panaji",
        "type": "Diesel"
    },
    {
        "date": "03-01-2018",
        "price": 64.46,
        "company": "Indian Oil",
        "city": "Panaji",
        "type": "Petrol"
    },
    {
        "date": "03-01-2018",
        "price": 60.61,
        "company": "Bharat Petroleum",
        "city": "Margao",
        "type": "Diesel"
    },
    {
        "date": "03-01-2018",
        "price": 64.25,
        "company": "Bharat Petroleum",
        "city": "Margao",
        "type": "Petrol"
    },
    {
        "date": "03-01-2018",
        "price": 60.64,
        "company": "Indian Oil",
        "city": "Margao",
        "type": "Diesel"
    },
    {
        "date": "03-01-2018",
        "price": 64.27,
        "company": "Indian Oil",
        "city": "Margao",
        "type": "Petrol"
    }
]

Using Gson u can achive with few lines of code ...`

 JSONArray jsonArray = response.getJSONArray("yourJsonArrayKeyNameHere");
                                Type listType = new TypeToken<List<YourModelClassForEachRowOfData>>() {
                                }.getType();
                                List<YourModelClassForEachRowOfData> dataList = (List<YourModelClassForEachRowOfData>) new Gson().fromJson(jsonArray.toString(),
                                        listType);


and finally send this List to your recyclerview adapter ..that's it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM