【问题标题】:Update recyclerView using JSON data wrt Id使用 JSON 数据 wrt Id 更新 recyclerView
【发布时间】:2018-10-19 05:49:18
【问题描述】:

我有下面的 json 来填充 recyclerview 项

{
"success": true,
"data": {
    "houses": [
        {
            "id": 1,
            "house_id": "HOUSEAGA00001",
            "ward_number": "23",
            "staus_id":1,
            "name_of_resident": "Melroy",
            "phone_number": "9890098900",
            "amount_due": 5000,
            "created_date": "2018-10-12T18:30:00.000Z",
            "updated_date": "2018-10-12T18:30:00.000Z"
        },
        {
            "id": 2,
            "house_id": "HOUSEAGA00002",
            "ward_number": "24",
            "staus_id":1,
            "name_of_resident": "Prajyot",
            "phone_number": "9823598235",
            "amount_due": 10000,
            "created_date": "2018-10-12T18:30:00.000Z",
            "updated_date": "2018-10-12T18:30:00.000Z"
        }
    ],
    "payment_statuses": [
        {
            "id": 1,
            "type": "Collected",
            "color_hex": "#00ff00"
        },
        {
            "id": 3,
            "type": "Owner unavailable",
            "color_hex": "#ff9900"
        },
        {
            "id": 2,
            "type": "Owner Denied",
            "color_hex": "#ff0000"
        }
    ]
},
"count": 2,
"error": []

}

我正在使用 house 数组来填充 recycler 视图项,但在 item 中有一个名为 status 的字段,我必须从 house 数组中获取状态 id,并从 payment_statuses 数组中获取其各自的类型。问题是我如何从回收站视图持有者中的房屋数组中获取状态支付状态数组中的类型。

【问题讨论】:

  • 提供代码。我们不是来提供解决方案的,而是帮助您解决您的代码遇到的问题。

标签: android android-recyclerview realmrecyclerviewadapter


【解决方案1】:

我认为这可能会有所帮助:How parse JSON in Android 特别是假设您在object 中拥有整个 JSON 对象:

获取具体的JSONArray

JSONObject jObject = object.getJSONObject("data"); 
JSONArray jArray = jObject.getJSONArray("houses");

从数组中获取项目

for (int i=0; i < jArray.length(); i++)
{
    try {
        JSONObject oneObject = jArray.getJSONObject(i);
        // Pulling items from the array
        int value = oneObject.getInt(
    } catch (JSONException e) {
        // Oops
    }
}

我没有测试代码,但想法是这样的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多