MWP MWP - 23 days ago 7
Android Question

(android)(firebase)Is it possible to load specific number of data on Firebase?(ChildEventListener)

Im making chat application with Firebase Realtime Database.
I Made

public class MessageAdapter extends BaseAdapter implements ChildEventListener{
...
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Message msg = dataSnapshot.getValue(Message.class);
this.addItem(msg);
this.notifyDataSetChanged();
}
...
}


and this MessageAdapter refresh my Listview when data is added to Firebase..
But

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Message msg = dataSnapshot.getValue(Message.class);
this.addItem(msg);
this.notifyDataSetChanged();
}



onChildAdded Method is called whenever I started app.. I want to load Message Only after User starting app. It loads every messages which is stored in my Firebase Realtime Database.


Is there a way to load only new message after user starting application?? and furthermore i want to make this to load specific number of messages like if i set the number as 5, when user start an app it loads from Firebase Database only 5 stored messages.

Answer

Okay, so there are a couple questions here, first would be to make the app wait before querying the data base. To do this I would suggest pausing the thread for a few seconds while it loads the data using a handler.

 handler1.postDelayed(new Runnable() {

     @Override
     public void run() {
        // Your Database Reference Listener
     }
}, 1000);

also to listen to only a few from firebase just change the listener like so

 yourDatabaseReferance.limitToFirst(10).addChildEventListener(new ChildEventListener()