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 usingsnapshot.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
How to Build a Native (Command Line) Executable to Run on Android
Fileprovider Crash - Npe Attempting to Invoke Xmlresourceparser on a Null String
How to Check If an App Is a Non-System App in Android
Android Fragment Onrestoreinstancestate
How to Iterate Through a View's Elements
Multiple Selection in Custom Listview with Cab
Programmatically Collapse a Group in Expandablelistview
How to Compile a Static Library Using the Android Ndk
How to Get Add to Home Screen Pop Up on Site Open in Mobile Browser
Android and Setting Alpha for (Image) View Alpha
Mkdir() Works While Inside Internal Flash Storage, But Not Sd Card
How to Upload Multipart Form Data and Image to Server in Android
Should I Use Android: Process =":Remote" in My Receiver
How to Display Menu Item with Icon and Text in Appcompatactivity