如何在 FirestoreRecyclerAdapter 中添加 2 个不同的查询? [英] How to add 2 different queries inside an FirestoreRecyclerAdapter?
问题描述
我有两个疑问:
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.
或者,看看 FirebaseUIs FirestorePagingAdapter
,它将(非实时)DocumentSnapshots
的多个页面组合在一个回收器视图中.
Alternatively, have a look at FirebaseUIs FirestorePagingAdapter
, which combines multiples pages of (non-realtime) DocumentSnapshots
in a single recycler view.
这篇关于如何在 FirestoreRecyclerAdapter 中添加 2 个不同的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!