13

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.
Data of firebase

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

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
  • https://github.com/milon87/ShakeTopUp/tree/master/app/src/main/java/xyz/enableit/shaketopup/offer you can check this. This has been implemented by MVP though. just check ModelOfferImpl.java file. Hope it will help. – Milon Feb 28 '17 at 09:53
  • Refer this to get the complete solution of this problem:- https://stackoverflow.com/a/44796538/3946958 – Ravindra Kushwaha Jul 03 '17 at 05:59
  • @RavindraKushwaha Is there any chance you can write a full answer in swift too, I mean put your whole code solution in swift? Thank you – bibscy Aug 22 '18 at 06:24
  • @bibscy Sorry , i only works on android..Will ask to my friend to help u..U can post a question here..Someone definitely help u – Ravindra Kushwaha Aug 22 '18 at 06:26
  • @RavindraKushwaha Could you please ask your friend to have a look at my question? https://stackoverflow.com/questions/51975438 – bibscy Aug 23 '18 at 10:14
  • @bibscy i have just sent the link to my friend.If he get any time than defiantly he will help u – Ravindra Kushwaha Aug 23 '18 at 10:18

2 Answers2

10

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.

kirtan403
  • 7,293
  • 6
  • 54
  • 97
  • Can you show example for your suggestion? Also, for some reason I'm using similar as you but I am getting: `java.lang.IllegalArgumentException: You must use startAt(String value), endAt(String value) or equalTo(String value) in combination with orderByKey(). Other type of values or using the version with 2 parameters is not supported` My code is `photoRerence.orderByKey().startAt(oldestPostId).limitToFirst(6).addListenerForSingleValueEvent(new ValueEventListener() {` – Tal C Aug 06 '17 at 23:14
  • @TalC It should work. Your error says the same thing as I mentioned first in my answer. Are you sure you are getting error at the same line you shared, because it seems fine to me. – kirtan403 Aug 07 '17 at 03:10
  • I am getting the error at that line. I am thinking that it could be due to the specific key not being loaded in time before it is called. This is due to me using a different library which is buggy in my app at the moment. So I initialized the library and then it immediately tries to request more info from Firebase which is why I am assuming it crashes. It does work if I manually put in a key. So it seems like I have to fix one bug in order to fix this bug. Hopefully that works. Thanks though! Fix one bug, get 99 more lol. – Tal C Aug 07 '17 at 06:35
  • @kirtan403 when specifying .startAt(oldestPostId), does the result includes the "oldestPostId" value in results as well? If yes, is there any way to tell firebase to something like startAfter(oldestPostId) ? – Sandip Fichadiya Dec 16 '17 at 12:11
  • @SandipSoni Yes, it includes the result. i don't think so, anything like startAfter exists. But you can ask for startAt and remove the first result. – kirtan403 Dec 18 '17 at 06:52
  • @kirtan403 yes that could be the way but to me it shouldn't be required. Anyways thanks for your answer :). Currently I am looking at Firestore which solves most of the limitation of firebase. – Sandip Fichadiya Dec 18 '17 at 06:58
3

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.

Milon
  • 2,221
  • 23
  • 27