问题
MY CURRENT CODES (now its working)
@Override
protected void onStart() {
super.onStart();
petshopRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
petshopsList.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Petshops petshops = postSnapshot.getValue(Petshops.class);
petshopsList.add(petshops);
}
CustomerPetshopAdapter adapter = new CustomerPetshopAdapter(CustomerViewPetshopActivity.this, R.layout.customlist_viewpetshop, petshopsList);
lvPetshops.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
QUESTION:
Lets say i have
petshopRef = FirebaseDatabase.getInstance().getReference("Petshops");
How do I retrieve all data in Blue Circle (in my image above)
回答1:
You can retrieve all the data using the once value event. Then you can loop through it to get each key and value. I'm writing this logic in nodejs, you can convert it into your java logic. Example,
petshopRef.once('value', function(snap){
snap.forEach(listData=>{ //list data will have top level auto generated keys
listData.forEach(element=>{
element.key // will be the key, in your case (brcat)
element.val() // will be the corresponding value to above key. in your case (3)
});
});
});
来源:https://stackoverflow.com/questions/50283866/firebase-database-retrieve-nested-data