Katz Katz -3 years ago 102
Android Question

Get last node in Firebase database Android

I want to get item in the last node added in firebase database from my Android. You can see on the image below i'm not sure how to get the specific node, because unique key is created by Firebase. How to reference to auto-created node and child inside? Thanks a lot

The last node

enter image description here

Answer Source

Try this:

DatabaseReference databaseReference = Firebase.getInstance().getReference();
Query lastQuery = databaseReference.child("mp").orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String message = dataSnapshot.child("message").getValue().toString();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        //Handle possible errors.
    }
});

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download