如何在Android studio中以Hetrogeneous形式的数据在RecylerView中设置Json数据

时间:2018-01-03 07:41:59

标签: android json android-studio android-recyclerview android-volley

这个json数据设置像android studio中的RecyelView列表

[
    {
        "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"
    }
]

1 个答案:

答案 0 :(得分:0)

使用Gson你可以用几行代码来实现...`

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


最后将此列表发送到您的recyclerview适配器..它是