gpt4 book ai didi

android - firebase 数据库读取嵌套列表错误

转载 作者:行者123 更新时间:2023-11-30 00:23:56 25 4
gpt4 key购买 nike

我正在尝试读取以下数据结构

   {
"posts" : {
"-KroPYVVmlkRXjiX1FhD" : {
"appliedUsers" : {
"flaged" : false,
"latitude" : 0,
"longitude" : 0,
"noFlags" : 0,
"noPosts" : 0,
"rating" : 0
},
"salary" : "5454564",
"title" : "زسروس",
}
},
"users" : {
"########" : {
"birthDate" : "8-18-1995",
"flaged" : false,
"image" : "https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/11018975_10153075514031013_8079824273778693128_n.jpg?oh=7d4a511abf155a65cda6dde4bfe7b180&oe=59F52E95",
"latitude" : 30.784419,
"longitude" : 30.987274,
"name" : "Aya Mohamed Hamed",
"noFlags" : 0,
"noPosts" : 0,
"rating" : 0,
"uid" : "########",
"userPhone" : "01007211319"
}
}
}

使用下面的 POJO

public class Job {

public Map<String, User> appliedUsers = new HashMap<>();
private String title;
private String salary;
// empty public constructor
// another constructor
// public getters and setters per each property

和用户 POJO 类

public class User  {

private String uid;
private String image;
private String name;
private String birthDate;
private String userPhone;
private long rating;
private int noPosts;
private int noFlags;
private String address;
private double longitude;
private double latitude;
private boolean isFlaged;
// empty public constructor
// another constructor
// public getters and setters per each property}

我的 Read Jobs 函数是:

 public static void getAllJobs(JobsDataChangeListener jobsDataChangeListener) {
postsDBRef = FirebaseDatabase.getInstance().getReference("posts");
postsDBRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ArrayList<Job> jobs = new ArrayList<>();
Logging.log(""+dataSnapshot.getValue());
Job job = dataSnapshot.getValue(Job.class);
jobs.add(job);
jobsDataChangeListener.onJobsDataChange(jobs, 1);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i(getClass().getName().toString(), ": " + databaseError.getMessage());
}
});

}

在我将应用的用户映射插入到 Pojo 对象之前,该功能一直可以很好地检索帖子,每次我尝试读取职位/帖子时,我都会遇到以下问题

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type com.jobease.www.jobease.models.User

最佳答案

appliedUsers 下的数据与您在代码中的定义不匹配。

appliedUsers
flaged: false
latitude: 0

在您的代码中,您将其映射到:

public Map<String, User> appliedUsers = new HashMap<>();

您没有分享 User 的定义,但它似乎不太可能是一种同时允许 boolean(对于 flaged ) 和一个数字(对于 latitude)。

当您尝试读取作业时,客户端会抛出错误,因为它无法将 0 转换为 User 对象。

更新

如果您在 appliedUsers 下拥有的是单个用户,您的定义应该是:

public class Job {
public User appliedUsers;
private String title;
private String salary;
...

关于android - firebase 数据库读取嵌套列表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45756315/

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