Images from the firebase
image1: image1
above image has parent:"products"
and child:"color","description"
image2: image2
above image continued image1
image3: image3
above image continued image2
image4: image4
above image continued image3
I want to access all the values inside the products like products have childs color,description,pic1,pic2,price and in each child there are multiple values stored I want to access all of them
CodePudding user response:
Actually you already get the value. You can do like this, more apprioriate.
databaseReference.child("color").addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot ds:snapshot.getChildren()){
final String key= ds.getKey();
final String pname= ds.getValue(String.class);
Log.d("talha5",pname);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.e("color","Error: " error.getMessage());
}
});
Also, you need to handle onCancelled
. Try to make as best practice.
UPDATE:
If you have many child. you need to separate them.
databaseReference.child("description").addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot ds:snapshot.getChildren()){
final String key= ds.getKey();
final String pname= ds.getValue(String.class);
Log.d("description",pname);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.e("description","Error: " error.getMessage());
}
});
Same goes to the others
databaseReference.child("pic1").addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot ds:snapshot.getChildren()){
final String key= ds.getKey();
final String pname= ds.getValue(String.class);
Log.d("pic1",pname);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.e("pic1","Error: " error.getMessage());
}
});