Get Last Item in nested database firebase android

Viewed 2246

This is my data structure: enter image description here

Here I want to retrieve the Latitude and Longitude of the last item of Location child based on the id = "sd".

Here's the code:

Query query = mDatabase.child("Users").orderByChild("Id").equalTo(busId);
// busId = "sd"
query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot user : dataSnapshot.getChildren()) {
                        Query lastItem = user.getRef().child("Location").orderByKey().limitToLast(1);
                        lastItem.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String lat = dataSnapshot.child("Latitude").getValue().toString();
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
                        break;
                    }
                }

How can I do that? Thanks

1 Answers
Related