user10507803
user10507803

Reputation:

Firebase realtime database: Is there a way to get value from different node?

My data structure is like this and what I want to do is to get value from different node.

root
  |__data__people___name1:...
         |        |_name2:...
         |        |_name3:...
         |         ...
         |_location__latitude:...
                   |_longitude:...

Now I want to get location's value (LatLng that is saved earlier) when people's child is added. But what I know is to get the value that is added. Is there a way to refer to different node value?

data.child("people").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            //get location's value here
            }
        }

Also, could you kindly tell me how to get LatLng from database and assign it to LatLng?

Upvotes: 0

Views: 79

Answers (1)

Janwilx72
Janwilx72

Reputation: 506

data.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
        {
           //This loops through the people node
           for( DataSnapshot snao : dataSnapshot.child("people").getChildren() )
           {}

           //get location's value here. Loops through Child nodes
           for( DataSnapshot locSnap : dataSnapshot.child("location").getChildren() )
           {}

           // OR for location
           double lat = (double)dataSnapshot.child("location").child("latitude").getValue();
           double lng = (double)dataSnapshot.child("location").child("longitude").getValue();
        }
    }

Upvotes: 1

Related Questions