【问题标题】:How to add SearchView query to FirestoreRecyclerOptions?如何将 SearchView 查询添加到 FirestoreRecyclerOptions?
【发布时间】:2023-04-03 14:06:01
【问题描述】:

我有一组聊天室,每个文档都包含一个名为 name 的字段,它表示聊天室的名称,另一个字段名为 timestamp,其中包含上次发送消息的时间戳。我创建了一个按时间戳排序的所有聊天室的回收者视图。我想添加对 SearchView 的支持。通过使用搜索,用户可以搜索他想要的聊天室。我试过了:

Query chatRoomsQuery = db.collection("chatRooms")
        .whereArrayContains("users", documentRef.getPath()).orderBy("timestamp", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<ChatRoom> options = new FirestoreRecyclerOptions.Builder<ChatRoom>().setQuery(chatRoomsQuery, ChatRoom.class).build();
setupRecyclerView(options);

chatSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        Query chatRoomsQuery = db.collection("chatRooms")
                .whereArrayContains("users", documentRef.getPath())orderBy("timestamp", Query.Direction.DESCENDING)
                .startAt("name",newText).endAt("name",newText + "\uf8ff");
        FirestoreRecyclerOptions<ChatRoom> options = new FirestoreRecyclerOptions.Builder<ChatRoom>().setQuery(chatRoomsQuery, ChatRoom.class).build();
        setupRecyclerView(options);
        return false;
    }
});

但是当我在搜索中输入一些字母时,我得到了一个例外:

java.lang.IllegalArgumentException:提供给 startAt() 的参数过多。参数的数量必须小于或等于 orderBy() 子句的数量。

如何查询不同字段的firebase云数据库?

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    startAt 和 endAt 不是 Cloud Firestore 查询中的搜索方式。

    您正在寻找:

    db.collection("chatRooms")
      .orderBy("name")
      .whereArrayContains("users", documentRef.getPath())orderBy("timestamp", Query.Direction.DESCENDING)
      .whereGreaterThanOrEqualTo("name", newText)
      .whereLessThanOrEqualTo("name", newText + "\uf8ff");
    

    另请参阅 query operators 上的 Firebase 文档。

    【讨论】:

    • 你得到You have an inequality where filter (whereLessThan(), whereGreaterThan(), etc.) on field 'name' and so you must also have 'name' as your first orderBy() field, but your first orderBy() is currently on field 'timestamp' instead。
    • 酷。那么,您是否添加了orderBy("name")?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2021-03-04
    相关资源
    最近更新 更多