How to get an array of strings from the firebase live database… here is a solution to the problem.
How to get an array of strings from the firebase live database
databaseReference = FirebaseDatabase.getInstance().getReference("/sample");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: dataSnapshot "+dataSnapshot.getValue());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
I’m also new to Android app development and Firebase. I’m getting data from the sample node and getting the DataSnapshot value as shown below.
{size=[Small, Large, Ex-Large], type=[Type 1, Type 2], color=[Red, Green, Blue], category=[T-Shirt, Jeans, Sweater]}
Need some expected help and any suggestions would be appreciated.
Thanks
Solution
To retrieve the values individually, you can use code like this:
databaseReference.child("category").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (int i=0; i<3; i++) {
category is an ArrayList you can declare above
category.add(dataSnapshot.child(String.valueOf(i)).getValue(String.class));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { // Do something for this
}
});
Similarly, you can add values for other nodes in other ArrayLists by changing the value of Childs in this code.