简体   繁体   中英

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 ?

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();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM