限制从 Firebase 获取数据以执行拉取刷新和加载更多功能 [英] Get the data from the Firebase in limit to perform pull to refresh and load more functionality

查看:22
本文介绍了限制从 Firebase 获取数据以执行拉取刷新和加载更多功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但现在我一次从 FireBase 获取所有数据.我想要做什么,在 LIMITS 中获取数据,例如 15 一次记录.就像第一次用户从 Firebase 获取 15 条记录一样,当用户在屏幕底部/顶部加载更多数据时,应该从 Firebase 获取更多的 15 条记录并添加到列表的底部/顶部.

我已经实现了从 Firebase 获取数据库顶部或底部的 15 条记录的逻辑,如下所示:-

公共类 ChatActivity 扩展 AppCompatActivity 实现 FirebaseAuth.AuthStateListener {私有 FirebaseAuth mAuth;私人数据库参考 mChatRef;私人查询 postQuery;私人字符串 newestPostId;私人字符串最旧的PostId;私人 int startAt = 0;私人 SwipeRefreshLayout swipeRefreshLayout;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_chat);mAuth = FirebaseAuth.getInstance();mAuth.addAuthStateListener(this);mChatRef = FirebaseDatabase.getInstance().getReference();mChatRef = mChatRef.child("聊天");/////获取滑动布局的视图IDswipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);/////从此处获取 FIREBASE 的前 10 条记录mChatRef.limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {@覆盖public void onDataChange(DataSnapshot dataSnapshot) {for (DataSnapshot child : dataSnapshot.getChildren()) {oldPostId = child.getKey();System.out.println("这里是数据==>>" + child.getKey());}}@覆盖public void onCancelled(DatabaseError databaseError) {}});//////为了拉来刷新代码在这里swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {@覆盖公共无效 onRefresh() {//刷新项目System.out.println("这里==>>>"+oldestPostId);///这里oldestPostId"是我从FIREBASE获得最后记录的关键mChatRef.startAt(oldestPostId).addListenerForSingleValueEvent(new ValueEventListener() {@覆盖public void onDataChange(DataSnapshot dataSnapshot) {for (DataSnapshot child : dataSnapshot.getChildren()) {System.out.println("这里添加数据后==>>" + child.getKey());}}@覆盖public void onCancelled(DatabaseError databaseError) {}});}});}

我在这里搜索过它,但没有得到预期的结果,下面是我搜索过的链接

1.

我已经实现了第一次获取 15 条或 10 条记录的逻辑并且它有效..并且还实现了在限制内加载更多记录的逻辑但是没有得到正确的解决方案(不工作),请帮忙告诉我我哪里做错了..谢谢:)

编辑解决方案

:- 我已经在此链接上实现了更多加载或拉动刷新功能:- Firebase 无限滚动列表视图在滚动时加载 10 个项目

解决方案

您缺少 orderByKey().对于任何过滤查询,您必须使用排序函数.请参阅文档

在您的 onRefresh 方法中,您需要设置限制:

 public void onRefresh() {//刷新项目///这里oldestPostId"是我从FIREBASE获得最后记录的关键mChatRef.orderByKey().startAt(oldestPostId).limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {.....

因此,您检索的数据是您获得前 10 条记录后仅有的 10 条新记录.

确保保存新检索数据中最旧的键,以便下次刷新时仅检索来自该键的新数据.

建议:除了添加子值侦听器来查找最后一个键之外,您可以只使用值侦听器并获取最后一个数据快照,其大小可以获取最后一条记录的键.>

Yet now i am getting the all data from the FireBase at one time.What i want to do that getting data in LIMITS like 15 records at a time. Like in first time user get the 15 records from the Firebase and when user load more data at the bottom/TOP of the screen than 15 more records should come from Firebase and added to the bottom/TOP of the list.

I have implemented the logic to get the 15 records at a top OR bottom of the database from Firebase like below:-

public class ChatActivity extends AppCompatActivity implements FirebaseAuth.AuthStateListener {

    private FirebaseAuth mAuth;
    private DatabaseReference mChatRef;

    private Query postQuery;
    private String newestPostId;
    private String oldestPostId;
    private int startAt = 0;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        mAuth = FirebaseAuth.getInstance();
        mAuth.addAuthStateListener(this);

        mChatRef = FirebaseDatabase.getInstance().getReference();
        mChatRef = mChatRef.child("chats");

         /////GETTING THE VIEW ID OF SWIPE LAYOUT
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);

    /////GETTING FIRST 10 RECORDS FROM THE FIREBASE HERE
        mChatRef.limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    oldestPostId = child.getKey();
                    System.out.println("here si the data==>>" + child.getKey());
                }      
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        //////FOR THE PULL TO REFRESH CODE IS HERE
       swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Refresh items  

             System.out.println("Here==>>> "+oldestPostId);

                ///HERE "oldestPostId" IS THE KEY WHICH I GET THE LAST RECORDS FROM THE FIREBASE

                mChatRef.startAt(oldestPostId).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot child : dataSnapshot.getChildren()) {

                    System.out.println("here AFTER data added==>>" + child.getKey());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

            }
        });
    }

I have searched here on SO for it , but did not get the expected result, below link which i have searched for it

1. First Link
2. Second Link
3. Third Link
4. Forth Link

Please look at my firebase data structure in image.

I have implemented the logic for the getting 15 OR 10 records at first time and it works..and also implemented the logic for loading more records in limits but not getting proper solution (NOT WORKING) , please help and let me know where am i doing wrong..Thanks :)

EDIT SOLUTION

:- I have implemented the load more or pull to refresh functionality on this link:- Firebase infinite scroll list view Load 10 items on Scrolling

解决方案

You are missing orderByKey(). For any filtering queries you must use the ordering functions. Refer to the documentation

In your onRefresh method you need to set the limit:

 public void onRefresh() {
     // Refresh items  
     ///HERE "oldestPostId" IS THE KEY WHICH I GET THE LAST RECORDS FROM THE FIREBASE
                mChatRef.orderByKey().startAt(oldestPostId).limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {
.....

So the data you retrieve is the only 10 new records after you got your first 10 records.

Make sure to save the oldest key of the newly retrieved data so that on next refresh new data from this key is only retrieved.

Suggestion: Instead of adding a child value listener to find the last key, you can just use the value listener and get the last data snapshot with the size to get the last record's key.

这篇关于限制从 Firebase 获取数据以执行拉取刷新和加载更多功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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