使用push的firebase android双数据

时间:2016-03-07 17:55:10

标签: android firebase firebase-realtime-database

我刚刚发现了Firebase并尝试创建一个小项目,其中保存并从数据库中获取数据,但我在这两个步骤中都有问题,当我在数据库中保存新对象时,我发现另一个“父节点“我附在屏幕下方。

当我去获取数据时,我得到空值,我认为这个问题与第一个问题,代码片段有关。

我该如何解决?

推送:

myFirebaseRef = new Firebase("https://theme.firebaseio.com/");
postRef = myFirebaseRef.child("theme");
postRef.push().setValue(theme);

阅读数据:

 myFirebaseRef.addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot snapshot) {
         Log.v("FIREBASE","There are " + snapshot.getChildrenCount() + " blog posts");
         for (DataSnapshot postSnapshot: snapshot.getChildren()) {
             Theme post = postSnapshot.getValue(Theme.class);                  System.out.println(post.getCatbg() + " - chiave" );
         }
     }
     @Override
     public void onCancelled(FirebaseError firebaseError) {
         System.out.println("The read failed: " + firebaseError.getMessage());
     }
});

主题类:

 @JsonIgnoreProperties(ignoreUnknown = true)
    public class Theme {
        private String a200;
        private String a500;
        private String a700;
        private String prim;
        private String primdark;
        private String setticon;
        private String b200;
        private String b500;
        private String b700;
        private String catbg;
        private String cattxt;
        private boolean dl;

        @JsonIgnore
        private String key;

        public Theme(){}

        public Theme(boolean defValue) {
            if(defValue) {
                String defaultColor = "#000000";
                this.a200 = defaultColor;
                this.a500 = defaultColor;
                this.a700 = defaultColor;
                this.prim = defaultColor;
                this.primdark = defaultColor;
                this.setticon = defaultColor;
                this.b200 = defaultColor;
                this.b500 = defaultColor;
                this.b700 = defaultColor;
                this.catbg = defaultColor;
                this.cattxt = defaultColor;
                this.dl = false;
            }
        }

        public Theme(String a200, String a500, String a700, String prim, String primdark, String setticon, String b200, String b500, String b700, String catbg, String cattxt, boolean dl) {
            this.a200=a200;
            this.a500=a500;
            this.a700=a700;
            this.prim=prim;
            this.primdark=primdark;
            this.setticon=setticon;
            this.b200=b200;
            this.b500=b500;
            this.b700=b700;
            this.catbg=catbg;
            this.cattxt=cattxt;
            this.dl=dl;
        }



        //Getter and setter using String

        public String getA200() {
            return a200;
        }
        public void setA200(String a200) {
            this.a200 = a200;
        }
        public String getA500() {
            return a500;
        }
        public void setA500(String a500) {
            this.a500 = a500;
        }
        public String getA700() {
            return a700;
        }
        public void setA700(String a700) {
            this.a700 = a700;
        }
        public String getPrim() {
            return prim;
        }
        public void setPrim(String prim) {
            this.prim = prim;
        }
        public String getPrimdark() {
            return primdark;
        }
        public void setPrimdark(String primdark) {
            this.primdark = primdark;
        }
        public String getSetticon() {
            return setticon;
        }
        public void setSetticon(String setticon) {
            this.setticon = setticon;
        }
        public String getB200() {
            return b200;
        }
        public void setB200(String b200) {
            this.b200 = b200;
        }
        public String getB500() {
            return b500;
        }
        public void setB500(String b500) {
            this.b500 = b500;
        }
        public String getB700() {
            return b700;
        }
        public void setB700(String b700) {
            this.b700 = b700;
        }
        public String getCatbg() {
            return catbg;
        }
        public void setCatbg(String catbg) {
            this.catbg = catbg;
        }
        public String getCattxt() {
            return cattxt;
        }
        public void setCattxt(String cattxt) {
            this.cattxt = cattxt;
        }

        public boolean getDl() {
            return dl;
        }
        public void setDl(boolean dl) {
            this.dl = dl;
        }

        //Getter and setter using int

        @JsonIgnore
        public int getA200AsInt() {
            return Color.parseColor(a200);
        }
        @JsonIgnore public int getA500AsInt() {
            return Color.parseColor(a500);
        }
        @JsonIgnore public int getA700AsInt() {
            return Color.parseColor(a700);
        }
        @JsonIgnore public int getPrimAsInt() {
            return Color.parseColor(prim);
        }
        @JsonIgnore public int getPrimdarkAsInt() {
            return Color.parseColor(primdark);
        }
        @JsonIgnore public int getSetticonAsInt() {
            return Color.parseColor(setticon);
        }
        @JsonIgnore public int getB200AsInt() {
            return Color.parseColor(b200);
        }
        @JsonIgnore public int getB500AsInt() {
            return Color.parseColor(b500);
        }
        @JsonIgnore public int getB700AsInt() {
            return Color.parseColor(b700);
        }
        @JsonIgnore public int getCatbgAsInt() {
            return Color.parseColor(catbg);
        }
        @JsonIgnore public int getCattxtAsInt() {
            return Color.parseColor(cattxt);
        }
    }

duplicated theme node

Json代码:

{
  "theme" : {
    "theme" : {
      "-KCH4MaQmSylOC9VRN4k" : {
        "a200" : "f48fb1",
        "a500" : "e91e63",
        "a700" : "c2185b",
        "b200" : "242424",
        "b500" : "131313",
        "b700" : "020202",
        "catbg" : "e91e63",
        "cattxt" : "212121",
        "dl" : false,
        "prim" : "323232",
        "primdark" : "212121",
        "setticon" : "424242"
      }
    }
  }
}

更新使用myFirebaseRef而不是postRef对象

{
  "-KCKhTfqBTkN2pMCbIs8" : {
    "test" : "7377f857-e433-4d21-a41e-1e2671f4b91b"
  },
  "-KCKhXAyczkgHWEwx3ur" : {
    "test" : "29d3753b-7352-455e-9367-a9bf2902bd36"
  }
}

0 个答案:

没有答案