【发布时间】:2020-10-24 10:58:01
【问题描述】:
我正在为教育学院开发应用程序,以下代码 sn-p 检索先前推送到 Firebase 实时数据库的 Parent 对象,以便在此 Student 对象推送到数据库之前链接到 Student 对象也..
如果这是该特定父母的第一个学生(孩子),则代码可以正常工作。 换句话说,这个 Parent 对象有一个他的孩子的列表。如果此列表为空或为空,则代码可以正常工作。但如果要推送的学生是同一个父级的第二个孩子,这意味着检索到的 Parent 对象将包含一个子级列表,我得到这个:
com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap
这是检索父对象的方法:
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());
}
});
}
数据库位于获取 dataSnapshot 值的行中:
mParent = snapshot.getValue(Parent.class);
这是数据库中的父节点: DatabaseReference
这是父对象的 POJO 类:
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;
}
}
【问题讨论】:
标签: java android firebase firebase-realtime-database