从 Firebase 检索父 POJO 中的 POJO 数组列表

Retrieving Arraylist of POJO in a parent POJO, from Firebase

我一直在努力从我的 Android 应用程序中的 firebase 检索数据,我无法理解如何检索具有多个子节点的节点。

情况是我在 firebase 'snaps' 中有一个节点,它有一个子节点 'likes' . 'snaps' 节点在用户上传照片时创建,'likes' 节点在用户喜欢照片时创建。每次照片被点赞时,一个 'liked-by' 键值对被推送到 snaps 的 likes 节点。

Objective: 使用 POJO 检索 snap 及其所有子 Likes

我尝试检索失败:

        Firebase ref = new Firebase(Constants.FIREBASE_URL+"snaps/");
    Query queryRef = ref.orderByChild("timestamp");

    queryRef.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot snapshot, String previousChild) {

            Snap snap = snapshot.getValue(Snap.class);
            snapsList.add(snap);

        }


    });

数据继承

第二个快照不包含喜欢个节点,因为它还没有被喜欢。

Snaps 的 POJO

public class Snap {
private String uid="";
private String uname="";
private String timestamp="";
private String pname="";
private String place="";
private String image="";
private String desc="";
private String likes="";

public Snap() {
}

public Snap(String uid, String uname, String timestamp, String pname, String place, String image, String desc) {
    this.uid = uid;
    this.uname = uname;
    this.timestamp = timestamp;
    this.pname = pname;
    this.place = place;
    this.image = image;
    this.desc = desc;
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getUname() {
    return uname;
}

public void setUname(String uname) {
    this.uname = uname;
}

public String getTimestamp() {
    return timestamp;
}

public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}

public String getPname() {
    return pname;
}

public void setPname(String pname) {
    this.pname = pname;
}

public String getPlace() {
    return place;
}

public void setPlace(String place) {
    this.place = place;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}
}

**数据表示- JSON **

{
              "snaps" : {
                "-KDJ0yinIYo-gKpVp13w" : {
                  "desc" : "a",
                  "image" : "...",
                  "likes" : {
                    "-KDJ11Bbbc_d1S2hU2Zk" : {
                      "liked_by" : "b64ff43c-7b51-40d1-aac8-6e7c700113e2"
                    }
                  },
                  "place" : "a",
                  "pname" : "a",
                  "timestamp" : "1458477456134",
                  "uid" : "b64ff43c-7b51-40d1-aac8-6e7c700113e2",
                  "uname" : "Dead Man"
                },
                "-KDJ19tOlJ8-W28k5YAD" : {
                  "desc" : "b",
                  "image" : "...",
                  "place" : "b",
                  "pname" : "b",
                  "timestamp" : "1458477506628",
                  "uid" : "b64ff43c-7b51-40d1-aac8-6e7c700113e2",
                  "uname" : "Dead Man"
                }
              },
              "users" : {
                "b64ff43c-7b51-40d1-aac8-6e7c700113e2" : {
                  "email" : "a@a.com",
                  "full_name" : "Dead Man",
                  "image" : "...",
                  "provider" : "password"
                }
              }
            }

用所需的结构定义一个 Likes class,然后在您的 Snap class 中,而不是 private String likes="";尝试私人喜欢喜欢;

类似于:

public class Snap
    {
      private Likes likes;

      public Likes getLikes ()
        {
            return likes;
        }

        public void setLikes (Likes likes)
        {
            this.likes = likes;
        }
    {

并为每个 children 个赞添加一个 class,使用相同的格式。