Reputation: 157
In my Firebase Database, I set it like this:
The latitude and longitude are represented by 0
and 1
respectively. I am currently using hard-coded method to obtain the latitude, longitude and name:
Firebase locationRef = mRootRef.child("loc");
locationRef.addValueEventListener(new com.firebase.client.ValueEventListener() {
@Override
public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {
Double latitude = dataSnapshot.child("0").getValue(Double.class);
Double longitude = dataSnapshot.child("1").getValue(Double.class);
String name = dataSnapshot.child("name").getValue(String.class);}
@Override
public void onCancelled(FirebaseError firebaseError) {
throw firebaseError.toException();
}
});
Now I set my data as shown in the image:
How can I obtain the data value of variable (0
, 1
and name
) for loc1 and loc2 in a manner that I can obtain them easily without hard-coding. The number of locations will increase in number too in the future too.
Upvotes: 0
Views: 1635
Reputation: 274
I think you need something like folowing
Need to add getChildren() foreach it will give your all location under loc you can take that info in some other array also
Firebase locationRef = mRootRef.child("loc");
locationRef.addValueEventListener(new com.firebase.client.ValueEventListener() {
@Override
public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {
for (DataSnapshot snapm: dataSnapshot.getChildren()) {
Double latitude = snapm.child("0").getValue(Double.class);
Double longitude = snapm.child("1").getValue(Double.class);
String name = snapm.child("name").getValue(String.class);}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
throw firebaseError.toException();
}
});
Upvotes: 1