【问题标题】:Firebase realtime database: Is there a way to get value from different node?Firebase 实时数据库:有没有办法从不同的节点获取价值?
【发布时间】:2018-10-22 13:38:57
【问题描述】:

我的数据结构是这样的,我想做的是从不同的节点获取价值。

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

现在我想在添加人们的孩子时获取位置的值(之前保存的 LatLng)。但我所知道的是获得增加的价值。有没有办法引用不同的节点值?

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

另外,您能否告诉我如何从数据库中获取LatLng 并将其分配给LatLng

【问题讨论】:

标签: java android firebase firebase-realtime-database


【解决方案1】:
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();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2017-03-26
    相关资源
    最近更新 更多