joe
joe

Reputation: 1371

Firebase child listener not showing any data

{
  "57c80ec0-e73f-49df-8296-25999e194288" : {
    "email" : "[email protected]",
    "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" : "[email protected]",
    "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"
  }
}

So, my idea is to show markers for all the hotels from each user in database and here I have a problem with querying. I'm not sure how to query each user.

This is what I tried but when I debug I get null for hotel.getName and 0 for lat and long..

I'd really appreciate if someone could tell me what I'm doing wrong or how to solve it..

This is my first question so I apologise if I did something wrong. :)

Cheers!

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) {

        }
    });

Upvotes: 1

Views: 2822

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598837

Under the /Users node that you're listening for, you have User nodes. The Hotel nodes are one level deeper.

To show the list of hotels for each user:

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

Upvotes: 2

Related Questions