Firebase儿童监听器未显示任何数据

时间:2016-04-05 18:48:39

标签: android firebase firebase-realtime-database

{
  "57c80ec0-e73f-49df-8296-25999e194288" : {
    "email" : "ss@zz.mm",
    "hotel" : {
      "-KEaYE1YfvPTRfgcO6Z_" : {
        "latit" : 20.15135303358223,
        "longit" : 23.86566750705242,
        "name" : "new marker"
      },
      "-KEaYGDhbFAN99GyY0cH" : {
        "latit" : 30.211601559876424,
        "longit" : -4.754576571285725,
        "name" : "new marker"
      },
      "-KEbjCjmx4-Xmr_1aVCy" : {
        "latit" : 20.58906906737089,
        "longit" : 25.271570831537247,
        "name" : "new marker"
      }
    },
    "password" : "nn",
    "username" : "ss"
  },
  "c01528ea-a22c-43de-b77a-bc833c5e394c" : {
    "email" : "ses@ses.ses",
    "hotel" : {
      "-KEaLImMODDRiDwK3gSo" : {
        "latit" : 18.160196227874213,
        "longit" : 26.094263345003128,
        "name" : "new marker"
      },
      "-KEaLKSEg1psa4xvggse" : {
        "latit" : 31.71881281252363,
        "longit" : -2.9933271557092667,
        "name" : "new marker"
      }
    },
    "password" : "ses",
    "username" : "ses"
  }
}

所以,我的想法是在数据库中显示每个用户的所有酒店的标记,这里我遇到查询问题。我不确定如何查询每个用户。

这是我尝试的但是当我调试时,我为hotel.getName获取null,为lat和long获取0 ..

如果有人能告诉我我做错了什么或如何解决它,我真的很感激..

这是我的第一个问题,所以如果我做错了,我会道歉。 :)

干杯!

private Firebase firebase = new Firebase("https://josip-my-application.firebaseio.com/Users/");

@Override
public void requestMarkers() {

Firebase hotelsRef = new Firebase("https://josip-my-application.firebaseio.com/Users/");
    com.firebase.client.Query queryRef = hotelsRef.orderByChild("hotel");
    queryRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Hotel hotel = dataSnapshot.getValue(Hotel.class);
            presenter.onMarkersLoaded(hotel.getName(), hotel.getLatit(), hotel.getLongit());

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

1 个答案:

答案 0 :(得分:2)

在您正在侦听的/Users节点下,您有用户节点。 Hotel个节点更深一层。

显示每位用户的酒店列表:

Firebase hotelsRef = new Firebase("https://josip-my-application.firebaseio.com/Users/");
com.firebase.client.Query queryRef = hotelsRef.orderByChild("hotel");
queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot userSnapshot, String s) {
        DataSnapshot hotelsSnapshot = userSnapshot.child("hotel");
        for (DataSnapshot hotelSnapshot: hotelsSnapshot.getChildren()) {
            Hotel hotel = hotelSnapshot.getValue(Hotel.class);
            presenter.onMarkersLoaded(hotel.getName(), hotel.getLatit(), hotel.getLongit());
        }
        // TODO: call adapter.notifyDataSetChanged();
    }