【问题标题】:Display list in recyclerView, sorted as per Date from JSON using retrofit在 recyclerView 中显示列表,使用改造按照 JSON 中的日期排序
【发布时间】:2018-07-07 08:12:13
【问题描述】:

所以,我正在制作一个需要显示交易列表的应用程序,我可以在其中获取交易的日期和时间,如下所示

 {
        "transaction_id": "7",
        "transaction_date": "2018-06-06 04:46:01.0",
        "amount": "499.50",
        "user_id": "2",
        "currency": "MXN",
        "associate_id": "2",
        "associate": {
            "is_active": "N",
            "associate_type": "asd",
            "name": "asd",
            "created_date": "12-Feb-2018 07:32:12 AM",
            "associate_id": "2",
            "url": null,
            "last_update_date": "12-Feb-2018 07:32:12 AM"
        },
        "card_id": "1",
        "card": null
    }

我正在使用recyclerView进行填充,现在我想显示列表如下;

那么,有人可以帮我完成这个过程吗?

【问题讨论】:

标签: android json retrofit2 android-recyclerview


【解决方案1】:

为实现Comparable interface 的响应创建一个模型类

这个类将覆盖 compareTo 方法,该方法将根据 transaction_date 对交易进行排序。

模型类

public class Transactions implements Comparable<Transactions> {

    @SerializedName("transaction_id")
    public String transactionId;

    @SerializedName("transaction_date")
    public String transactionDate;

    @SerializedName("amount")
    public String amount;

    @SerializedName("user_id")
    public String userId;

    @SerializedName("currency")
    public String currency;

    @SerializedName("associate_id")
    public String associateId;

    @SerializedName("associate")
    public Associate associate;

    @SerializedName("card_id")
    public String cardId;

    @SerializedName("card")
    public Object card;

    public class Associate {

        @SerializedName("is_active")
        public String isActive;

        @SerializedName("associate_type")
        public String associateType;

        @SerializedName("name")
        public String name;

        @SerializedName("created_date")
        public String createdDate;

        @SerializedName("associate_id")
        public String associateId;

        @SerializedName("url")
        public Object url;

        @SerializedName("last_update_date")
        public String lastUpdateDate;

    }

    @Override
    public int compareTo(Transactions transaction) {
        return new Date(this.transactionDate).compareTo(new Date(transaction.transactionDate));
    }
}

在将数据传递给适配器类之前,使用Collection framework API对列表进行排序

ArrayList<Transactions> transactionList = new ArrayList<Transactions>();  

Collections.sort(transactionList);

希望对你有帮助

【讨论】:

  • 感谢您的及时建议,我会努力申请的。
  • 希望您了解解决方案。如有任何疑问,请随时回信
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 1970-01-01
相关资源
最近更新 更多