我在我的应用中使用Firestore。当我从那个数据库中获取数据时,我将数据获取到documentSnapShot
对象中,但是当我将documentSnapShot
转换为自定义对象时,它被初始化了,但是其中不包含任何数据。
我的Firebase Firestore数据库:
我的POJO类: 类FirebaseData
public class FirebaseData {
private List<Group> group = null;
/**
* No args constructor for use in serialization
*
*/
public FirebaseData() {
}
/**
*
* @param group
*/
public FirebaseData(List<Group> group) {
super();
this.group = group;
}
public List<Group> getGroup() {
return group;
}
public void setGroup(List<Group> group) {
this.group = group;
}
}
班组
public class Group {
private String name;
private List<Sticker> stickers = null;
private Integer id;
/**
* No args constructor for use in serialization
*
*/
public Group() {
}
/**
*
* @param name
* @param stickers
* @param id
*/
public Group(String name, List<Sticker> stickers, Integer id) {
super();
this.name = name;
this.stickers = stickers;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Sticker> getStickers() {
return stickers;
}
public void setStickers(List<Sticker> stickers) {
this.stickers = stickers;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
班级贴纸
public class Sticker {
private String name;
private String id;
/**
* No args constructor for use in serialization
*
*/
public Sticker() {
}
/**
*
* @param name
* @param id
*/
public Sticker(String name, String id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
MainActivity类:
db.collection("PackGroups").document("1").get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
Log.d("TAG : firebase" ,"onSuccess: "+documentSnapshot.getData());
FirebaseData firebaseData = documentSnapshot.toObject(FirebaseData.class );
recyclerView.setAdapter(new AdapterGroupPack(MainActivity.this, firebaseData.getGroup()));
recyclerView.getAdapter().notifyDataSetChanged();
}
});
答案 0 :(得分:1)
您的代码中的问题是List<Group> group
类中的FirebaseData
字段是从小写字母开始声明的,而数据库中的大写字母是(Group)。为了使其正常工作,两个名称必须匹配。为了解决这个问题,您可以更改数据库中该数组的名称以小写字母开头,或者在getter前面添加以下注释:
@PropertyName("Group")
public List<Group> getGroup() {
return group;
}