Get the data from the Firebase in limit to perform pull to refresh and load more functionality

前端 未结 2 1990
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 09:54

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 tim

相关标签:
2条回答
  • 2020-12-01 10:31

    restructure your database, set a new child id which is incremental like 0,1,2,3... etc

    "chats": {
    "-KZLKDF": {
      "id": 0,
      "message": "Hi",
      "name":"username"
    },
    

    then make a method for query

    public void loadFromFirebase(int startValue,int endValue){
    
    mChatRef.orderByChild(id).startAt(startValue).endAt(endValue).addListenerForSingleValueEvent(this);
    }
    

    make sure you have implemented addListenerForSingleValueEvent then do adapter related task. Initially call from onCreate method:

    loadFromFirebase(0,10);
    

    NB: loading new content in the list you have to be aware with adapter.

    0 讨论(0)
  • 2020-12-01 10:36

    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.

    0 讨论(0)
提交回复
热议问题