作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的应用中使用 Firestore。当我从该数据库获取数据时,我将数据获取到 documentSnapShot
对象中,但是当我将 documentSnapShot
转换为自定义对象时,它会被初始化,但其中不包含任何数据.
我的 Firebase Firestore 数据库:
我的 POJO 类(class):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;
}
}
类(class)组
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;
}
}
类(class)贴纸
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();
}
});
最佳答案
您代码中的问题是List<Group> group
提交已在您的 FirebaseData
中声明类以小写字母开头,而在数据库中以大写字母(组)开头。为了使其正常工作,两个名称必须匹配。要解决此问题,您可以更改数据库中该数组的名称以小写字母开头,或者在 getter 前面添加以下注释:
@PropertyName("Group")
public List<Group> getGroup() {
return group;
}
关于java - 在 documentSnapShot 中获取值,但将其转换为对象时,对象返回 null,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60862606/
我是一名优秀的程序员,十分优秀!