Akmal Nizar Rosle
Akmal Nizar Rosle

Reputation: 15

Firebase Database Reference for Retrieving Data

I log in as a User B (3wgmOhWlUUcyoBQXKZWnhw3K7Ik2) and I want to retrieve location data of User A (yKlWu19vhqQXfL2tDlBNfMSduMe2) from the database.

Here my database structure look like.

{
  "3wgmOhWlUUcyoBQXKZWnhw3K7Ik2" : {
      "name" : "MAMA",
      "type" : "P"
  },
  "yKlWu19vhqQXfL2tDlBNfMSduMe2" : {
      "location" : {
          "latitude" : 3.0537728,
          "longitude" : 101.4826099
      },
      "name" : "AYAH NAN",
      "type" : "D"
  }
}

How can I write for the Firebase Database Ref to retrieve the location data? It is correctly this?

databaseReference = FirebaseDatabase.getInstance().getReference().child("yKlWu19vhqQXfL2tDlBNfMSduMe2");

I should retrieve by ChildListener() or straight using getValue()?

Upvotes: 1

Views: 10240

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598901

Here's one way to read the latitude:

databaseReference = FirebaseDatabase.getInstance().getReference().child("yKlWu19vhqQXfL2tDlBNfMSduMe2");
databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
      double lat = dataSnapshot.child("location/latitude").getValue(Double.class);
    }

I highly recommend also reading the Firebase Database documentation on reading data and then taking the Firebase codelab for Android.

Upvotes: 2

Related Questions