Issue
When I try to show comments in the list I got this error –
com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToParameterizedType(CustomClassMapper.java:251)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:177)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
My comment showing activity –
myRef.child("Photo")
.child(mphoto.getPhoto_id())
.child("comments")
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
Log.d(TAG, "onChildAdded: child added.");
Query query = myRef
.child("Photo")
.orderByChild("photo_id")
.equalTo(mphoto.getPhoto_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dsnapshot) {
for ( DataSnapshot singleSnapshot : dsnapshot.getChildren()){
Photo photo = new Photo();
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
photo.setCaption(objectMap.get("caption").toString());
photo.setTags(objectMap.get("tags").toString());
photo.setPhoto_id(objectMap.get("photo_id").toString());
photo.setUser_id(objectMap.get("user_id").toString());
photo.setDate_Created(objectMap.get("date_Created").toString());
photo.setImage_Path(objectMap.get("image_Path").toString());
mComments.clear();
Comments firstComment = new Comments();
firstComment.setUser_id(mphoto.getUser_id());
firstComment.setComment(mphoto.getCaption());
firstComment.setDate_created(mphoto.getDate_Created());
mComments.add(firstComment);
List<Comments> commentsList = new ArrayList<Comments>();
for (DataSnapshot dSnapshot : singleSnapshot
.child("comments").getChildren()){
Comments comments = new Comments();
comments.setUser_id(dSnapshot.getValue(Comments.class).getUser_id());
comments.setComment(dSnapshot.getValue(Comments.class).getComment());
comments.setDate_created(dSnapshot.getValue(Comments.class).getDate_created());
mComments.add(comments);
}
photo.setComments(mComments);
mphoto=photo;
setupWidgets();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Solution
The error says
Expected a List while deserializing, but got a class java.util.HashMap
This might getting the error
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
You can change this and make it simple like below
String caption = singleSnapshot.child("caption").getValue(String.class);
photo.setCaption(objectMap.get("caption").toString());//So you wont able to do this
photo.setCaption(caption);//And you can use like this
Instead of using this
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
photo.setCaption(objectMap.get("caption").toString());
photo.setTags(objectMap.get("tags").toString());
photo.setPhoto_id(objectMap.get("photo_id").toString());
photo.setUser_id(objectMap.get("user_id").toString());
photo.setDate_Created(objectMap.get("date_Created").toString());
photo.setImage_Path(objectMap.get("image_Path").toString());
Replace it with this
String caption=singleSnapshot.child("caption").getValue(String.class);
String tags= singleSnapshot.child("tags").getValue(String.class);
String photo_id=singleSnapshot.child("photo_id").getValue(String.class);
String user_id= singleSnapshot.child("user_id").getValue(String.class);
String date_Created = singleSnapshot.child("date_Created").getValue(String.class);
String image_Path= singleSnapshot.child("image_Path").getValue(String.class);
photo.setCaption(caption);
photo.setTags(tags);
photo.setPhoto_id(photo_id);
photo.setUser_id(user_id);
photo.setDate_Created(date_Created);
photo.setImage_Path(image_Path);
As you can see no objectMap
Answered By – Cyrille Con Morales
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0