5

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

Sagaryal
  • 415
  • 4
  • 15
  • Here's the documentation for android [queries](https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot) – Devid Farinelli Jul 24 '17 at 08:58
  • Could you please explain a bit more. I could not understand it. I am very new to it. I've update the code to review. Thanks. – Sagaryal Jul 24 '17 at 09:15
  • Well done! I'm not an android expert, I've always used javascript, but I'll try to take a look at this ;) – Devid Farinelli Jul 24 '17 at 09:24

1 Answers1

4

Please use this code for Android:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String Id = ds.child("Id").getValue(String.class);
            if(Id.equals("sd")) {
                Query q = ds.child("Location").getRef().orderByKey().limitToLast(1);

                ValueEventListener valueEventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for(DataSnapshot child : dataSnapshot.getChildren()) {
                            Double latitude = child.child("Latitude").getValue(Double.class);
                            Double longitude = child.child("Longitude").getValue(Double.class);
                            Log.d("TAG", latitude + " / " + longitude);
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {}
                };
                q.addListenerForSingleValueEvent(valueEventListener);
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
userRef.addListenerForSingleValueEvent(eventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • The for loop is not executed. It doesnot go inside for loop. It might be because it is empty. Any Idea? – Sagaryal Jul 25 '17 at 16:28
  • Please see my updated answer. I did not actually test it, just code it. Does it work? – Alex Mamo Jul 25 '17 at 16:41
  • I get java.lang.NullPointerException in "String lat = dataSnapshot.....".toString() error stating toString() is reference to null object which means that line giving null. And after that app stops. So I think for loop is necessary. I always get null pointer. – Sagaryal Jul 26 '17 at 12:50
  • Changed the String latitude -> Double latitude and longitude. Tell me, if you add `Log.d("TAG", Id);` after the declaration of `String Id` what do you get? Or is Id null? – Alex Mamo Jul 26 '17 at 13:06
  • Id is fine. I do not get null. But when Log.d("getref()", q.getref().toString()). I get referenced to https://sth.firebaseio.com/Users/-Kpn_-aKX8ZGLahYC5yh/Location Should not I be reference to again deeper down to key of last item? – Sagaryal Jul 26 '17 at 13:21
  • Also, If I Log.d() just above Double latitude as... Log.d("tag", dataSnapshot.getRef().toString()), the it is reference to same link as I mentioned in above comment. – Sagaryal Jul 26 '17 at 13:23
  • Yes, you are right. I have added a new loop that loops trough the childrens. Please see again my updated answer. Does it work? – Alex Mamo Jul 26 '17 at 13:37
  • Yeah, Thanks that worked now. But I have one issue here. Having 2 for loops won't the performance degrade? I am trying to get realtime geolocation which changes in 5-10 seconds. Won't it be slow? – Sagaryal Jul 26 '17 at 13:54