Reputation: 43
I'm working on an app for an educational academy, the following snippet of code retrieves a Parent object that was previously pushed to Firebase Realtime Database to be linked to a Student object before this Student object is pushed to the database too..
If this is the first student (child) to this particular parent, the code works fine.. In other words, this Parent object has an List of his child(ren). If this list is empty or null, the code works fine.. but if the Student to be pushed is a second child to the same parent, which means the retrieved Parent object will contain a list of children, I get this:
com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap
Here's the method that retrieves the Parent object:
private void getCorrespondingParent(){
DatabaseReference correspondingParentReference = FirebaseDatabase.getInstance().getReference()
.child("parents").child(mUserPhone);
correspondingParentReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
mParent = snapshot.getValue(Parent.class);
// null out the children field because the Parent object will be saved in the Student object
// this avoids having a parent with children list that each Student in it has a parent object that
// has a list of students...
mParent.setChildren(null);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w(LOG_TAG, "loadPost:onCancelled", error.toException());
}
});
}
the database is in the line that gets the value of the dataSnapshot:
mParent = snapshot.getValue(Parent.class);
here's the Parent node in the database: DatabaseReference
and here's the POJO class for the Parent object:
public class Parent {
private int accType;
private String name;
private String uid;
private String phone;
private String whatsappPhone;
private String know;
private List<Student> children;
public Parent() {}
public Parent(String name, String uid, String phone, String whatsappPhone, String know) {
this.accType = 3;
this.name = name;
this.uid = uid;
this.phone = phone;
this.whatsappPhone = whatsappPhone;
this.know = know;
}
public int getAccType() {
return accType;
}
public void setAccType(int accType) {
this.accType = accType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getWhatsappPhone() {
return whatsappPhone;
}
public void setWhatsappPhone(String whatsappPhone) {
this.whatsappPhone = whatsappPhone;
}
public String getKnow() {
return know;
}
public void setKnow(String know) {
this.know = know;
}
public List<Student> getChildren() {
return children;
}
public void setChildren(List<Student> children) {
this.children = children;
}
}
Upvotes: 0
Views: 104
Reputation: 43
Okay I found out what was the problem..
As shown in the Parent object node
I use student name as key to the Student object, and also used List to store the children of the parent in the Parent object.. turns out you can't use custom keys for that purpose if you're using a list, a list item key is it's index only and it can't be changed.. I switched to Map<String, Student> children
instead of List<Student> children
and now it works fine.
No changes needed in the code for pushing:
DatabaseReference newChildReference = FirebaseDatabase.getInstance().getReference()
.child("parents").child(parentPhone).child("children").child(childKey);
newChildReference.setValue(mNewStudent);
newChildReference.push();
mNewStudent is a Student object that holds the newly added student data.
Upvotes: 1