I'm trying to get a list of users from Firebase, in Android.
I already have the uuid of the user, and using that I'm trying to get the entire object. The uuid is both the node name and a field inside the user node (identical, of course).
Here is my code:
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("users");
Query query = usersRef.orderByChild("uuid").equalTo(animalMarker.getUserUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: xxxxxxxx---- entered onDataChange");
// go to node "users/userUID" , becuase dataSnapshot is parent node
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Log.d(TAG, "xxxxxxxx---- onDataChange: ds= " + ds);
User user = ds.getValue(User.class);
Log.d(TAG, "xxxxxxxx---- onDataChange: user=" + user);
// createdByTV.setText("Created by \n" + user.getEmail());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
I dont know if the error "expected List but got HashMap" is coming from the entire DataSnapshot itself, or from the animals List inside the user ( The user class has a field List animals )
I've already checked this similar question Andorid-Expected a List while deserializing, but got a class java.util.HashMap and several others but I still can't understand what is wrong.
EDIT: I'm trying to get the user email, knowing it's ID(uuid). While getting the entire User object is not necessary, I'm curios as to how it would be done, since I can't manage to do either of these.
EDIT: here is my User class
public class User {
private String uuid;
private String email;
private String name;
private List<User> friends;
private List<Animal> animals;
public User() {
}
public User(String email, String name) {
this.email = email;
this.name = name;
}
public User(String uuid, String email, String name) {
this.uuid = uuid;
this.email = email;
this.name = name;
}
... // getters and setters, toString and other such
}
When trying to get the User which doesn't have the animals node inside it, everything works fine. but when trying to get the user with the animals node inside, the app crashes.