有没有办法通过使用 FirestoreRecyclerAdapter 组合查询游标来对查询进行分页? [英] Is there a way to paginate queries by combining query cursors using FirestoreRecyclerAdapter?

查看:30
本文介绍了有没有办法通过使用 FirestoreRecyclerAdapter 组合查询游标来对查询进行分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的查询:

I have a query that looks like this:

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

这就是我向 RecyclerView 显示数据的方式.

This is how I display the data to my RecyclerView.

firestoreRecyclerOptions = new FirestoreRecyclerOptions.Builder<ModelClass>().setQuery(query, ModelClass.class).build();
myFirestoreRecyclerAdapter = new MyFirestoreRecyclerAdapter(firestoreRecyclerOptions);
recyclerView.setAdapter(myFirestoreRecyclerAdapter);

我正在尝试使用 此处 而我无法解决它.

I'm trying to use pagination as specified in here and I cannot solve it.

first.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot documentSnapshots) {
        DocumentSnapshot lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
        Query second = ref.orderBy("name", Query.Direction.ASCENDING).startAfter(lastVisible).limit(10);
        //Create a firestoreRecyclerOptions and setting the adapter
    }
});

有没有办法通过使用 FirestoreRecyclerAdapter 组合查询游标来对查询进行分页?这甚至可能吗?

Is there a way to paginate queries by combining query cursors using FirestoreRecyclerAdapter? Is this even possible?

推荐答案

正如@FrankvanPuffelen 已经在之前的您的问题,您无法实现,因为在您的情况下,您应该传递 2 个不同的查询(firstsecond) 到单个适配器,这在 FirestoreRecyclerAdapter 中是不可能的.您可以将第一个查询或第二个查询与您的适配器的单个实例一起使用.

As @FrankvanPuffelen already answered in an earlier question of yours, you cannot achieve that because in your case, you should pass 2 different queries (first and second) to a single adapter, which is not possible with FirestoreRecyclerAdapter. You can either use the first query or the second with a single instance of your adapter.

一个解决方案是创建两个不同的列表,包含每个查询的结果并将它们组合起来.然后你可以将结果列表传递给另一个适配器,假设是一个 ArrayAdapter,然后将结果显示到一个 ListView 或更好的 RecyclerView.这种情况下的问题是您将无法使用 FirestoreRecyclerAdapter 类提供的实时功能,但这种方法可以解决您的问题.

A solution would be to create two different lists, containing the results from each query and combine them. Then you can pass the resulting list to another adapter, let's say an ArrayAdapter and then display the results into a ListView or even better in a RecyclerView. The problem in this case is that you will not be able to use the real-time features that the FirestoreRecyclerAdapter class provides but this approach will solve your problem.

根据您在评论部分的要求,我将举例说明如何使用 ListViewArrayAdapter<以最简单的方式对按钮单击时的查询进行分页/代码>.向下滚动时,您也可以使用 RecyclerView 来实现相同的目的.但为了简单起见,假设我们有一个 ListView 和一个 Button,并且我们希望在每次单击按钮时将更多项目加载到列表中.为此,让我们首先定义视图:

According to your request from the comment section, I'll give you an example on how to paginate a query on button click in the easiest way, using a ListView and an ArrayAdapter. You can achieve the same thing also using a RecyclerView when scrolling down. But to keep things simple, let's assume we have a ListView and a Button and we want to load more items to the list on every button click. For that, let's define first the views :

ListView listView = findViewById(R.id.list_view);
Button button = findViewById(R.id.button);

假设我们有一个如下所示的数据库结构:

Let's assume we have a database structure that looks like this:

Firestore-root
   |
   --- products (collection)
         |
         --- productId (document)
                |
                --- productName: "Product Name"

还有一个看起来像这样的模型类:

And a model class that looks like this:

public class ProductModel {
    private String productName;

    public ProductModel() {}

    public ProductModel(String productName) {this.productName = productName;}

    public String getProductName() {return productName;}

    @Override
    public String toString() { return productName; }
}

现在,让我们定义一个限制设置为 3 的查询.

Now, let's define a query with the limit set to 3.

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference productsRef = rootRef.collection("products");
Query firstQuery = productsRef.orderBy("productName", Query.Direction.ASCENDING).limit(3);

这意味着在每次点击按钮时,我们都会再加载 3 个项目.现在,这是发挥魔力的代码:

This means that on every button click, we'll load 3 more items. And now, here is the code that does the magic:

firstQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            List<ProductModel> list = new ArrayList<>();
            for (DocumentSnapshot document : task.getResult()) {
                ProductModel productModel = document.toObject(ProductModel.class);
                list.add(productModel);
            }
            ArrayAdapter<ProductModel> arrayAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, list);
            listView.setAdapter(arrayAdapter);
            lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Query nextQuery = productsRef.orderBy("productName", Query.Direction.ASCENDING).startAfter(lastVisible).limit(3);
                    nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> t) {
                            if (t.isSuccessful()) {
                                for (DocumentSnapshot d : t.getResult()) {
                                    ProductModel productModel = d.toObject(ProductModel.class);
                                    list.add(productModel);
                                }
                                arrayAdapter.notifyDataSetChanged();
                                lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);
                            }
                        }
                    });
                }
            });
        }
    }
});

其中 lastVisible 是一个 DocumentSnapshot 对象,表示查询中的最后一个可见项.在这种情况下,每隔三个,它被声明为全局变量:

In which lastVisible is a DocumentSnapshot object that represents the last visibile item from the query. In this case, every third one and it is declared as gloabl variable:

private DocumentSnapshot lastVisible;

编辑 2:这里您还有关于如何从 Firestore 获取数据的解决方案数据库并在用户滚动时以较小的块显示在 RecyclerView 中.

Here you have also the solution on how you can get the data from your Firestore database and display it in smaller chunks in a RecyclerView when user scrolls.

这篇关于有没有办法通过使用 FirestoreRecyclerAdapter 组合查询游标来对查询进行分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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