A. Newbie
A. Newbie

Reputation: 187

How to retrieve last child from Firebase (java)?

DatabaseReference lastRef = FirebaseDatabase.getInstance().getReference().child("Ranklist");
Query lastQuery = lastRef.orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot data) {
        newUser.Rank=Long.parseLong(data.getKey().toString())+1;

        OrderConsoleMessages();
        texts[0].setText("Last Rank is: "+newUser.Rank+" | UID: " + data.getValue());
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        OrderConsoleMessages();
        texts[0].setText(databaseError.toString());
    }
});

My question is how do I retrieve the last child from "Ranklist"? I tried to orderByKey ByValue but nothing works in my case. I don't get any errors, I think I simply don't get any data from this query.

https://image.ibb.co/joKwCe/image.png

Edit: This is what I get from data.getValue().toString ->>> {4=somekey4}

Upvotes: 1

Views: 3178

Answers (3)

Faiizii Awan
Faiizii Awan

Reputation: 1710

your code is working correctly. I tested it. Your query to database returning the last child of the Parent Node "Ranklist". make your DatabaseReference dbref as global member

dbRef = FirebaseDatabase.getInstance().getReference().child("Ranklist");
Query lastQuery = dbRef.orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
     for(DataSnapshot data : dataSnapshot.getChildren())
     {
       //if you call methods on dataSnapshot it gives you the required values
       String s = data.getValue(); // then it has the value "somekey4" 
       String key = data.getKey(); // then it has the value "4:"
       //as per your given snapshot of firebase database data 
      }
}

@Override
public void onCancelled(DatabaseError databaseError) {

}});

Hope it is helping...

Happy Coding :-)

Upvotes: 1

Mayur Patel
Mayur Patel

Reputation: 2326

Your code is correct but you are getting only Key of the Last Node. So you need to change it with like:

dbRef.child("Ranklist")
 .orderByChild(Constants.ARG_CONV_TIMESTEMP)
 .limitToLast(1)
 .addChildEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String value = dataSnapshot.getValue(); 
        String key = dataSnapshot.getKey(); 
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }});

Upvotes: 1

Alex Mamo
Alex Mamo

Reputation: 138824

You cannot use a query in this case. As I see in your screenshot, the Ranklist node is an ArrayList and to get the value of your last child under Ranklist node, which is somekey4, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Ranklist");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ArrayList<String> arrayList = (ArrayList<String>) dataSnapshot.getValue();
        String lastItem = arrayList.get(arrayList.size() - 1);
        Log.d("TAG", lastItem);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
ref.addListenerForSingleValueEvent(valueEventListener);

The output in your logcat will be: somekey4.

Upvotes: 0

Related Questions