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

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

问题描述

我有一个如下所示的查询:

I have a query that looks like this:

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

这是我向 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个不同的查询(第一个第二个)到单个适配器, 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.

编辑:

根据您在评论部分提出的请求,我会举个例子关于如何使用 ListView ArrayAdapter 以最简单的方式对按钮点击分页查询。向下滚动时,您也可以使用 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 look 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; }
}

现在,让我们定义一个限制设置为<$ c $的查询c> 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 对象,表示查询中的最后一个visibile项。在这种情况下,每三个一个,它被声明为gloabl变量:

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

private DocumentSnapshot lastVisible;

Edit2:此处 您还可以获得有关如何从Firestore数据库获取数据并在<$ c中以较小块显示的解决方案用户滚动时$ c> RecyclerView 。

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

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