gpt4 book ai didi

java - 在 documentSnapShot 中获取值,但将其转换为对象时,对象返回 null,为什么?

转载 作者:行者123 更新时间:2023-12-02 08:49:41 24 4
gpt4 key购买 nike

我在我的应用中使用 Firestore。当我从该数据库获取数据时,我将数据获取到 documentSnapShot 对象中,但是当我将 documentSnapShot 转换为自定义对象时,它会被初始化,但其中不包含任何数据.

我的 Firebase Firestore 数据库:

1

我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com