Android Firebase Database How To Retrieve All Data From Child Node
Solution 1:
You're creating a reference with this:
database = FirebaseDatabase.getInstance();myRef = database.getReference().child("Sold").child("Item");
And then you add a ChildEventListener
. That means that the DataSnapshot
in your onChildAdded
will be called with a snapshot of the DUMMY 1
node first, and then with a snapshot of the DUMMY 2
node.
You're calling dataSnapshot.getValue(String.class)
in onChildAdded
. But since DUMMY 1
and DUMMY 2
have multiple properties, they don't have a single string value. So that call returns null
, which is probably what's causing you problems.
If you want to add the DUMMY 1
, DUMMY 2
key itself to the adapter, you can call dataSnapshot.getKey()
. Otherwise you can get the value of the specific child properties with dataSnapshot.child("Description").getValue(String.class)
.
So:
myRef.addChildEventListener(newChildEventListener() {
@OverridepublicvoidonChildAdded(@NonNull DataSnapshot dataSnapshot, @NullableString s) {
String key = dataSnapshot.getKey();
String description = dataSnapshot.child("Description").getValue(String.class);
Long quantity = dataSnapshot.child("Quantity").getValue(Long.class);
myArrayList.add(key+": "+description+" ("+quantity+")");
myArrayAdapter.notifyDataSetChanged();
}
If you'd like to read the entire value into a class representing the item (a so-called POJO), the simplest class is:
publicclassItem{
public String Description;
publicLong Quantity;
}
Note that the name (including the case) of the fields must exactly match the names of the properties in your database. You can use the above class like this:
myRef.addChildEventListener(newChildEventListener() {
@OverridepublicvoidonChildAdded(@NonNull DataSnapshot dataSnapshot, @NullableString s) {
String key = dataSnapshot.getKey();
Item item = dataSnapshot.getValue(Item.class);
myArrayList.add(key+": "+item.description+" ("+item.quantity+")");
myArrayAdapter.notifyDataSetChanged();
}
Solution 2:
The problem is that you cannot convert the dataSnapshot you are receiving into a String.
So it would be bettor to do like this:
myRef.addChildEventListener(newChildEventListener() {
@OverridepublicvoidonChildAdded(@NonNull DataSnapshot dataSnapshot, @NullableString s) {
for (DataSnapshot dummys : dataSnapshot.getChildren()) {
String description = dummys.child("Description").getValue(String.class);
int Quantity = dummys.child("Quantity").getValue(Integer.class);
//Use your variables here
}
}
Or if you want to create a object of Dummy:
publicclassDummy {
StringDescription;
IntegerQuantity;
publicDummy(String description, Integer quantity) {
Description = description;
Quantity = quantity;
}
publicStringgetDescription() {
returnDescription;
}
publicvoidsetDescription(String description) {
Description = description;
}
publicIntegergetQuantity() {
returnQuantity;
}
publicvoidsetQuantity(Integer quantity) {
Quantity = quantity;
}
}
myRef.addChildEventListener(newChildEventListener() {
@OverridepublicvoidonChildAdded(@NonNull DataSnapshot dataSnapshot, @NullableString s) {
List<Dummy> dummyList = newArrayList<>();
for (DataSnapshot dummys : dataSnapshot.getChildren()) {
dummyList.add(dummys.getValue(Dummy.class));
}
}
Post a Comment for "Android Firebase Database How To Retrieve All Data From Child Node"