【问题标题】:Sort after retrieving data from Firestore but before populating recyclerview在从 Firestore 检索数据之后但在填充 recyclerview 之前进行排序
【发布时间】:2020-07-13 06:52:59
【问题描述】:

问题:

我将事件(想想日历事件)存储在 Firestore 中,并将时间存储为字符串,因为 Firestore 中的时间戳似乎是别的东西。前任。上午 10:00、晚上 10:00 等。我想在我的RecyclerView 中按时间排序显示这些事件。

是否可以在填充RecyclerView 之前对其进行排序?我尝试在网上寻找并没有找到任何东西,并且不知道如何解决这个问题。

我正在使用 FirebaseUI,我猜这必须发生在适配器中的某个地方

Query query = firebaseFirestore.collection(year).whereIn("location", "Park").orderBy("time", Query.Direction.ASCENDING);
                    //Recycler options
                    FirestoreRecyclerOptions < EventModel > options = new FirestoreRecyclerOptions.Builder < EventModel > ()
                            .setQuery(query, EventModel.class)
                            .build();

                    eventAdapter = new EventAdapter(options);
                    mFirestoreList.setAdapter(eventAdapter);
                    eventAdapter.startListening();
                    eventAdapter.notifyDataSetChanged();

【问题讨论】:

  • 您可以使用Date 数据类型来存储日期。 FirebaseFirestore 支持对Date 字段的查询和操作。

标签: java android firebase android-recyclerview google-cloud-firestore


【解决方案1】:

如果您想在查询 Firestore 数据库时获得准确的结果,您应该使用 java Date 或 Firestore Timestamp 对象,但绝对不是字符串。我是说,因为 String 对象的排序方式是lexicographic,所以你得到的结果不会有太大帮助。

为了能够添加时间戳值,您可以使用我的以下答案中的解决方案:

ServerTimestamp is always null on Firebase Firestore

不应更改您实际使用的查询,它将按现在的方式工作。

【讨论】:

  • 嗨大卫!我可以帮助您了解其他信息吗?如果您认为我的回答对您有所帮助,请考虑通过单击左侧投票箭头下方的复选标记(✔️)来接受它。应将颜色更改为绿色。我真的很感激。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多