根据 Firebase 的最新日期将数据排序到 RecyclerView [英] Sort data to RecyclerView based on latest date from Firebase

查看:19
本文介绍了根据 Firebase 的最新日期将数据排序到 RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Android 聊天应用程序,我需要在其中按日期排序对话详细信息.下面提到了我的 firebase 数据结构.

I am developing an Android chat application in which I need to order the conversation details by the date. My firebase data structure is mentioned below.

现在我想根据时间戳从 Firebase 实时数据库中检索并显示 RecyclerView 上最新日期的数据.

Now I want to retrieve and show the data on the latest date on my RecyclerView from Firebase Realtime Database based on timestamp.

我尝试了以下方法.

final DatabaseReference nm =
        FirebaseDatabase.getInstance().getReference("Transaction");

Query query = nm.orderByChild("Date").limitToFirst(5);
;

query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        listData.clear();
        if (dataSnapshot.exists()) {
            for (DataSnapshot npsnapshot : dataSnapshot.getChildren()) {
                Transaction ld = npsnapshot.getValue(Transaction.class);

                listData.add(ld);
            }
            Tadapter = new TransactionAdapter(listData);
            rv.setAdapter(Tadapter);
            Log.d(TAG, "Total Count" + Tadapter.getItemCount());
        }
    }
}

推荐答案

我正在开发一个 Android 聊天应用程序,我需要在其中按日期对对话详细信息进行排序.

I am developing an android chat application in which I need to order the conversation details by the date.

正如我在您的屏幕截图中看到的,您的 Date 属性是字符串类型.这意味着您不能调用:

As I see in your screenshot, your Date property is of type String. This means that you cannot call:

.orderByChild("Date")

并期望表现得像时间戳一样.当您对 String 元素进行排序时,您获得的顺序始终是字典序.这意味着 Strings 在排序时不考虑任何类型的数值,尤其是在涉及日期时,即使日期包含数字,就像您的第一个元素一样:

And expect to behave as it was a Timestamp. When you order String elements, the order that you get is always lexicographically. This means that Strings doesn't consider any sort of numeric values when sorting, especially when it comes to the dates, even if the dates contain numbers, as your first element does:

Date: "30/7/2021"

因此,在查询数据库时使用字符串值不是一种选择.但是,我看到您已经拥有 Timestamp 属性.也许在那个属性上,它应该进行排序.如果情况并非如此,我建议您将 Date 属性的类型从 String 更改为 Timestamp,如我在以下帖子中的回答中所述:

So using String values when querying your database it's not an option. However, I see you already have a Timestamp property. Maybe on that property, it was supposed to do the ordering. If that was not the case, I suggest you change the type of the Date property from String to Timestamp, as explained in my answer from the following post:

现在我想在我的 RecyclerView 上检索和显示最新日期的数据

Now I want to retrieve and show the data on the latest date on my RecyclerView

这意味着您很可能需要颠倒顺序,这意味着您的所有交易都必须在您的 RecyclerView 中降序显示.在这种情况下,您可以在服务器或客户端上有几个选项.

This means that most likely you need to reverse the order, meaning that all your transactions have to be displayed in your RecyclerView descending. In this case, there are a few options that you have, either on the server or on the client.

假设您已将 Date 属性的类型从 String 更改为 Timestamp,那么您可以简单地考虑存储倒置的 Timestamp 值,如下所示:

Assuming that you have changed the type of your Date property from String to Timestamp, then you can simply consider storing an inverted Timestamp value like this:

Firebase-root
  |
  --- transactions
        |
        --- 1
            |
            --- Date: 1627714194
            |
            --- invertedDate: -1627714194

看,invertedDate 属性持有一个负值.由于默认情况下,元素按升序排列,为了能够对事务进行降序排列,您应该简单地使用:

See, the invertedDate property holds a negative value. Since by default, the elements are ordered ascending, to be able to order the transaction desecendiong, you should simply use:

Query query = nm.orderByChild("invertedDate").limitToFirst(5);

另一方面,有一些变通方法可以在客户端上实现相同的目标,如我在以下帖子中的回答中所述:

On the other hand, there are some workarounds that can be made to achieve the same thing on the client, as explained in my answer from the following post:

这篇关于根据 Firebase 的最新日期将数据排序到 RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆