【问题标题】:How to club dates together and show their data in java or in kotlin如何将日期组合在一起并在 java 或 kotlin 中显示他们的数据
【发布时间】:2020-01-22 18:40:57
【问题描述】:

我正在 android 中检索这个 json,我想按日期显示数据(带有 viewpager 的标签)

例如- 23(Tab1): 1) id-9854359 2) id-9854360 || 24(Tab2) id-9854361 2) id-9854360

每个选项卡都有我想要显示数据的 recyclerview 项目,但问题是我想将日期组合在一起并据此显示数据。我已经尝试过使用 hashmap、map...但仍然没有运气。

[
    {
    "end_time": "2020-01-23 01:45:00+00:00",
    "is_booked": false,
    "is_expired": false,
    "slot_id": 9854359,
    "start_time": "2020-01-23 01:30:00+00:00",
    "username": null
    },
    {
    "end_time": "2020-01-23 02:05:00+00:00",
    "is_booked": false,
    "is_expired": false,
    "slot_id": 9854360,
    "start_time": "2020-01-23 01:50:00+00:00",
    "username": null
    },
    {
    "end_time": "2020-01-24 02:45:00+00:00",
    "is_booked": false,
    "is_expired": false,
    "slot_id": 9854359,
    "start_time": "2020-01-24 02:30:00+00:00",
    "username": null
    },
    {
    "end_time": "2020-01-24 03:05:00+00:00",
    "is_booked": false,
    "is_expired": false,
    "slot_id": 9854359,
    "start_time": "2020-01-24 02:50:00+00:00",
    "username": null
    }
    ]

【问题讨论】:

    标签: java android kotlin hashmap set


    【解决方案1】:

    将 JSON 转换为类。据我所知,在 Android 中使用 GSON 是标准做法。

    class Entity {
        String end_time
        boolean is_booked
        boolean is_expired
        long slot_id;
        String start_time;
        String username;
    }
    
    List<Entity> entities = gson.fromJson(yourJsonAsString), Entity.class);
    

    提取日期并按日期分组。我建议使用 java Stream API,因为它仅用于以下任务:

    Map<String, List<Entity>> grouppedByDate = 
        entities.stream().collect(
            Collectors.grouppingBy(entity -> entity.end_time.split(" ")[0])
        )
    

    使用包含列表视图的每个选项卡创建选项卡视图。

    对于选项卡适配器,请使用集合 grouppedByDate.entries(),其中 entry.getKey() 是此选项卡中实体的日期,entry.getValue() 是条目列表。

    【讨论】:

    • 嘿伙计,我们如何创建时间(早上、下午、晚上)和列表 的地图?
    • @hiashutoshsingh 你必须传递一个接受实体并将"morning","afternoon" or "evening"返回到grouppingBy的函数
    • @hiashutoshsingh 更好:为 MORNING 定义一个枚举...而不是使用字符串
    • @alijazerzen 谢谢,我想通了。
    猜你喜欢
    • 2021-06-05
    • 2015-12-21
    • 2016-01-09
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多