Vlad Muresan Vlad Muresan - 5 months ago 32
Android Question

How to iterate trough a Firebase structure in Android ?

So i want to create a function that iterates trough a firebase structure looking like this :

{
"users" : {
"7e122736-2dd4-4770-a360-a0e7cbe41a43" : {
"currentLatitude" : 46.6598714,
"currentLongitude" : 23.5637339,
"displayName" : "gjsj2",
"email" : "2@vlad.com",
"password" : "123"
},
"a09e7e1d-ad3a-4d21-b0ba-069e0999bb93" : {
"currentLatitude" : 47.6599014,
"currentLongitude" : 23.5636797,
"displayName" : "gjsj",
"email" : "1@vlad.com",
"password" : "123"
},
"abc29286-fd6d-4088-95da-759828b5835d" : {
"currentLatitude" : 50.6599043,
"currentLongitude" : 23525.5637188,
"displayName" : "gjsj3",
"email" : "3@vlad.com",
"password" : "123"
}
}
}


I want to create a function that takes every unique user id's child and uses his coordinates to create a marker on a google maps map. Here's what i got so far but it doesnt seem to be working :

public void onChildChanged(DataSnapshot snapshot, String s) {
for (DataSnapshot userSnapshot : snapshot.getChildren()) {
for (DataSnapshot uniqueUserSnapshot : userSnapshot.getChildren()) {
Users currentUser = uniqueUserSnapshot.getValue(Users.class);

MarkerOptions options = new MarkerOptions()
.title(currentUser.getEmail())
.icon(BitmapDescriptorFactory.defaultMarker(getRandomNumberInRange(0, 360)))
.position(new LatLng(currentUser.getCurrentLatitude(), currentUser.getCurrentLongitude()));
mMap.addMarker(options);
Toast.makeText(MainActivity.this, "fsafasfasfas", Toast.LENGTH_SHORT).show();
}
}

}


And here's my Users POJO :

package com.licenta.vladut.mmap;
public class Users {
String email;
String displayName;
String password;
double currentLatitude;
double currentLongitude;


public Users() {
}

public Users(String email, String displayName, String password, double currentLongitude, double currentLatitude) {
this.email = email;
this.displayName = displayName;
this.password = password;
this.currentLongitude = currentLongitude;
this.currentLatitude = currentLatitude;
}
public Users(String email, String displayName, String password) {
this.email = email;
this.displayName = displayName;
this.password = password;

}

public String getDisplayName() {
return displayName;
}

public String getEmail() {
return email;
}

public String getPassword() {
return password;
}
public double getCurrentLongitude() {
return currentLongitude;
}

public double getCurrentLatitude() {
return currentLatitude;
}


}

Would love to get your ideas. Thank you.

Answer

If I understand the question correctly, you are actually close to what you want to do. Reference the database child user, and then add a ValueEventListener:

Firebase userRef = new Firebase(/*url*/);
userRef = userRef.child("users");
userRef.addValueEventListener(new ValueEventListener(){
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot uniqueUserSnapshot : dataSnapshot.getChildren()) {
            ...
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

ValueEventListener returns the node called, not children one by one.