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.
Actually, I answered that question but in this case the problem is a little different. So you are getting the following error:
expected List but got HashMap
Because your animals
node that exist within each user object is not a list of animal objects is actually a map of animal objects and that's why this error.
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.
It's true, you can get the email address simply using the following line of code:
String email = ds.child("email").getValue(String.class);
Now, to map a DataSnapshot
object to a User
and get a list of animal objects from a particular user, please remove:
//private List<User> friends;
//private List<Animal> animals;
And use the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference animalsRef = rootRef.child("users").child(uid).child("animals");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Animal> animalList = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Animal animal = ds.getValue(Animal.class);
animalList.add(animal);
}
//Do what you need to do with the animalList
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
animalsRef.addListenerForSingleValueEvent(valueEventListener);