How to Retrieve a List Object from the Firebase in Android

Get object with list from firebase on Android

Try this :

@SerializedName("bags")
private Map<String,Bag> bags;

How to retrieve a list of objects from Firebase

You User class needs to include following (assuming you have ChatRoom java class for example):

Map<String,ChatRoom> chatRoomList;

How to retrieve full List Of Objects from Firebase that contains another List of String in it?

Please use this code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference studentsListRef = rootRef.child("studentsList");
Query query = studentsListRef.orderByChild("name");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
String key1 = ds.child("batch").child("0").getValue(String.class);
String key2 = ds.child("batch").child("1").getValue(String.class);
Log.d("TAG", name + " / " + key1 + " / " + key2);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(eventListener);

In this way, you'll get the value of name and of both fields under batch.

How can I retrieve an object from the Firebase RealTime Database if it contains a List?

I've adapted the answer Frank and Alex mentioned in their comments. And it seems to be working in your situation. Instead of directly using
snapshot.getValue(HostPlayer.class); use a GenericTypeIndicator like this;

    pChildEventListener = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
GenericTypeIndicator<HostPlayer> t = new GenericTypeIndicator<HostPlayer>() {};
HostPlayer hostPlayer = snapshot.getValue(t);//I was able to get the list without an error
...
}
...
};


Related Topics



Leave a reply



Submit