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

时间:2017-08-18 12:09:06

标签: android firebase firebase-realtime-database

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

   {
  "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}

我的阅读工作功能是:

 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());
            }
        });

    }

该功能正在检索帖子,直到我将应用的用户Map插入Pojo对象,每次我尝试阅读作业/帖子时,我都会遇到以下问题

  

com.google.firebase.database.DatabaseException:无法转换对象   类型java.lang.Long类型com.jobease.www.jobease.models.User

2 个答案:

答案 0 :(得分:1)

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

appliedUsers
  flaged: false
  latitude: 0

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

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

您没有分享User的定义,但它似乎不太可能同时允许booleanflaged)和数字({{1} }})。

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

<强>更新

如果您在User下拥有的是单个用户,则您的定义应为:

appliedUsers

答案 1 :(得分:0)

试试这个:

FirebaseDatabase.getInstance()
                .getReference()
                .child("posts")
                .addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        Job job = dataSnapshot.getValue(Job.class);
                    }

                    @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) {

                    }
                });