How to access a child inside real time database array child?
String count = item_count.getText().toString();
firebaseDatabase = FirebaseDatabase.getInstance();
reference = firebaseDatabase.getReference();
reference.child("beverages").child(????).child("item_count").setValue(count);
this is my code but i don't know what should i put in the question marks.
]1
CodePudding user response:
You can get the item_count
by using foreach
loop inside the onDataChange
method.
- Lets say this is your model class for your
Beverage
.
public class Beverage {
private long item_count;
private String item_img;
private String item_name;
public Beverage beverage(long itemCount){
this.item_count = itemCount;
//and do others also...
}
//Here your get set...
}
- Here you get the data as
arrayList
reference.child("beverages").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
ArrayList<Beverage> beveragesList = new ArrayList<Beverage>();
//Get children
for (DataSnapshot snapShot : dataSnapshot.getChildren()) {
if (snapShot.exists()) {
final Beverage beverage = snapShot.getValue(Beverage.class);
if (beverage != null){
//Her you got the data as array list
beverageList.add(beverage);
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i("???","Db error: " databaseError.getMessage());
}
});
CodePudding user response:
You need to put child of beverages here like 0,1,2, or 3.
reference.child("beverages").child("3").child("item_count").setValue(count);