从Firebase

时间:2016-03-20 13:05:09

标签: android firebase firebase-realtime-database

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

情况是我在firebase ' snaps' 中有一个节点,其中有一个子节点'喜欢' 。 '快照'当用户上传照片并且喜欢'用户喜欢照片时创建节点。每次喜欢这张照片时,都会喜欢这张照片。键值对被推送到快照'喜欢节点。

目的: 要使用POJO检索快照及其所有孩子喜欢

我尝试检索失败:

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

        }


    });

数据heirachy enter image description here

第二个快照不包含节点,因为它尚未被喜欢。

扣' 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"
                }
              }
            }

1 个答案:

答案 0 :(得分:5)

使用所需的结构定义一个Likes类,然后在Snaps类中,而不是私有String likes =“”;尝试私人喜欢;

类似的东西:

public class Snap
    {
      private Likes likes;

      public Likes getLikes ()
        {
            return likes;
        }

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

并使用相同的格式为喜欢的每个孩子添加一个类。