I have a collection of users and I want to query all users from the database and display them in a RecyclerView except one, mine. This is my db schema:

users [collection]
  - uid [document]
     - uid: "fR5bih7SysccRu2Gu9990TeSSyg2"
     - username: "John"
     - age: 22
  - //other users

如何像这样查询数据库:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query q = db.collection("users").whereNotEqualTo("uid", uid);

所以我需要将这个查询对象传递给一个FirestoreRecyclerOptions对象,以便显示RecyclerView中的所有其他用户.

Is this even possible? If not, how can I solve this? Thanks!

Edit:

options = new FirestoreRecyclerOptions.Builder<UserModel>()
        .setQuery(query, new SnapshotParser<UserModel>() {
            @NonNull
            @Override
            public UserModel parseSnapshot(@NonNull DocumentSnapshot snapshot) {
                UserModel userModel = documentSnapshot.toObject(UserModel.class);
                if (!userModel.getUid().equals(uid)) {
                    return userModel;
                } else {
                    return new UserModel();
                }
            }
        }).build();

推荐答案

After days and days of struggling with this issue, I finally found the answer. I could not solve this without the help of @Raj. Thank you so much @Raj for the patience and guidance.

First off all, according to the answer provided by @Frank van Puffelen in his answer from this post, I stopped searching for a solution that can help me pass two queries to a single adapter.

在这个问题中,我想要实现的就是查询数据库以获得除我之外的所有用户.因此,因为我们不能将两个查询组合成一个实例,所以我发现我们可以将两个查询的结果组合在一起.因此,我创建了两个查询:

FirebaseFirestore db = FirebaseFirestore.getInstance();
Query firstQuery = db.collection("users").whereLessThan("uid", uid);
Query secondQuery = db.collection("users").whereGreaterThan("uid", uid);

I'm having a UserModel (POJO) class for my user object. I found not one, but two ways to solve the problem. The first one would be to query the database to get all user objects that correspond to the first criteria and add them to a list. After that, query the database again and get the other user objects that correspond to the second criteria and add them to the same list. Now I have a list that contains all the users that I need but one, the one with that particular id from the queries. This is the code for future visitors:

firstQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        List<UserModel> list = new ArrayList<>();
        if (task.isSuccessful()) {
            for (DocumentSnapshot document : task.getResult()) {
                UserModel userModel = document.toObject(UserModel.class);
                list.add(userModel);
            }

            secondQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (DocumentSnapshot document : task.getResult()) {
                            UserModel userModel = document.toObject(UserModel.class);
                            list.add(userModel);
                        }

                        //Use the list of users
                    }
                }
            });
        }
    }
});

The second approach would be much shorter because I use Tasks.whenAllSuccess() like this:

Task firstTask = firstQuery.get();
Task secondTask = secondQuery.get();

Task combinedTask = Tasks.whenAllSuccess(firstTask, secondTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
        @Override
        public void onSuccess(List<Object> list) {
            //This is the list that I wanted
        }
});

Kotlin相关问答推荐

UByte范围. Min_UTE.. UByte.MAX_UTE不符合预期

如何在Jetpack Compose中从领域查询中读取数据?

在Kotlin中,我是否可以访问已知的WHEN子句值?

在 map 中查找键与在 kotlin 中查找 firstOrNull

循环中的每个元素都应填充行中可用的所有可用空间

如何缩短 MaterialTheme.colors.primary?

KMM:编译失败:意外的 IrType 类型:KIND_NOT_SET

Kotlin 中私有集的完整语法 struct 是什么?

如何在kotlin中使用协程每秒调用一个函数

java - 如何将函数作为参数从java传递给kotlin方法?

为什么 Kotlin 需要函数引用语法?

IntelliJ 不会根据 ktlint 的期望对 Kotlin 导入进行排序

使用 Kotlin 创建新目录,Mkdir() 不起作用

如何在 Kotlin 中为变量设置监听器

在 Koin 中提供一个 Instance 作为其接口

Kotlin DataBinding 将静态函数传递到布局 xml

Ktor 在 java.library.path 中没有 netty_transport_native_epoll_x86_64

Kotlin out-projected 类型禁止使用

如何计算Kotlin中的百分比

从 Kotlin 访问 Integer.class