Here is my Database Reference:
firebaseDatabase = FirebaseDatabase.getInstance();
myRef = (DatabaseReference) firebaseDatabase.getReference().child("Notifications").orderByChild("Date");
Here is a snapshot of my Firebase console:
Here is my Database Reference:
firebaseDatabase = FirebaseDatabase.getInstance();
myRef = (DatabaseReference) firebaseDatabase.getReference().child("Notifications").orderByChild("Date");
Here is a snapshot of my Firebase console:
You can retrieve recent data as follow:
List<Notifications> notificationList = new ArrayList();
firebaseDatabase = FirebaseDatabase.getInstance();
myRef = (DatabaseReference) firebaseDatabase.getReference().child("Notifications").orderByKey();
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Notifications notification = snapshot.getValue(Notifications.class);
notificationList.add(0,notification);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
With firebase you should do some of the query logic client side. descending order and ascending orders are not available on Firebase.
Here is what I do to retrieve data from Firebase:
1) Make helping methods in order to attach and detach your Listener. This code will be called whenever something has changed in your real time database.
// Firebase instance variables
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mNotificationsDatabaseReference;
private ChildEventListener mChildEventListener;
private void attachDatabaseReadListener() {
if (mChildEventListener == null) {
mChildEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Notification currentNotification= dataSnapshot.getValue(Notification.class); //deserialize data (make it an object again)
mNotificationAdapter.add(currentNotification);
//Store your data here in what order you like.
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
public void onCancelled(DatabaseError databaseError) {
}
};
mNotificationsDatabaseReference.addChildEventListener(mChildEventListener);
}
}
private void detachDatabaseReadListener() {
if (mChildEventListener != null) {
mNotificationsDatabaseReference.removeEventListener(mChildEventListener);
mChildEventListener = null;
}
}
2) Don't forget to attach and detach the listener onResume and onPause.
@Override
protected void onResume() {
super.onResume();
attachDatabaseReadListener();
}
@Override
protected void onPause() {
super.onPause();
mNotificationAdapter.clear(); //Delete data in adapter which populates a listview. Otherwise you will end up with double items.
detachDatabaseReadListener();
}
For extra information and help you should read the official documentation
and if you really want to understand the basics of Firebase I recommend you see this amazing tutorial (made from Google).