如何在FirestoreRecyclerAdapter内添加2个不同的查询? [英] How to add 2 different queries inside an FirestoreRecyclerAdapter?

本文介绍了如何在FirestoreRecyclerAdapter内添加2个不同的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个查询:

Query firstQuery = ref.orderBy("name", Query.Direction.ASCENDING).limit(10);
getData(firstQuery);

Query secondQuery = ref.orderBy("price", Query.Direction.ASCENDING).limit(10);
getMoreData(secondQuery);

第一种方法如下:

private void getData(Query query) {
    firestoreRecyclerOptions = new FirestoreRecyclerOptions.Builder<ModelClass>().setQuery(query, ModelClass.class).build();
    myFirestoreRecyclerAdapter = new MyFirestoreRecyclerAdapter(firestoreRecyclerOptions);
    recyclerView.setAdapter(myFirestoreRecyclerAdapter);
}

这是第二种方法.

private void getMoreData(Query query) {
    firestoreRecyclerOptions = new FirestoreRecyclerOptions.Builder<ModelClass>().setQuery(query, ModelClass.class).build();
    myFirestoreRecyclerAdapter = new MyFirestoreRecyclerAdapter(firestoreRecyclerOptions);
    recyclerView.setAdapter(myFirestoreRecyclerAdapter);
}

两个变量都声明为全局变量:

Both variables are declared as global:

private FirestoreRecyclerOptions<ModelClass> firestoreRecyclerOptions;
private MyFirestoreRecyclerAdapter myFirestoreRecyclerAdapter;

启动应用程序时,使用第一种方法将元素显示在RecyclerView中.我要实现的是单击按钮时,触发getMoreData()方法以在同一适配器中添加第二个查询的结果,最终有20个元素.现在,当我单击按钮时,第二个查询中的元素将覆盖第一个查询.

When the app starts, the elements are displayed in the RecyclerView using the first method. What I want to achieve is that on a button click, when the getMoreData() method is triggered to add the result from the second query in the same adapter, ending up having 20 elements. Now, when I click the button, the elements from the second query will override the first ones.

推荐答案

FirestoreRecyclerAdapter中,没有内置的函数可以组合两个查询.

There is nothing built-in to combine two queries in a FirestoreRecyclerAdapter.

我能想到的最好的方法是在应用程序代码中创建合并结果的List/array,然后使用数组适配器.这是不理想的,因为您将不会使用FirebaseUI.

The best I can think of is creating a List/array of the combined results in your app code and then using an array adapter. It's not ideal, since you won't be using FirebaseUI.

或者,看看

Alternatively, have a look at FirebaseUIs FirestorePagingAdapter, which combines multiples pages of (non-realtime) DocumentSnapshots in a single recycler view.

这篇关于如何在FirestoreRecyclerAdapter内添加2个不同的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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