Firebase自定义Java对象 [英] Firebase to custom Java object

查看:136
本文介绍了Firebase自定义Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
users:{
mchen:{
friends:{brinchen:true},
name:Mary Chen,
widgets:{one:true,three:true}
},
brinchen:{...} ,
hmadi:{...}
}
}



如何为上面的例子编写自定义对象类? Firebase中的指南只显示一个简单的例子。和往常一样,您需要一个将JSON中的每个属性映射到Java类的Java类一个字段+ getter:

  static class User {
String name;
Map< String,Boolean>朋友;
Map< String,Boolean>小部件;

public User(){}

public String getName(){return name; }
public Map< String,Boolean> getFriends(){return friends; }
public Map< String,Boolean> getWidgets(){返回小部件; }
$ b $ @覆盖
public String toString(){
returnUser {+
name ='+ name +'\''+
,friends =+ friends +
,widgets =+ widgets +
'}';






$ b我真的只是按照这些指令从关于Android数据阅读的Firebase指南


我们将创建一个代表博客文章的Java类。与上次一样,我们必须确保我们的字段名称与Firebase数据库中的属性名称相匹配,并为该类提供默认的无参数构造函数。


然后你可以像这样加载信息:

  Firebase ref = new Firebase(https:// stackoverflow.firebaseio.com/34882779/users); (DataSnapshot userSnapshot:dataSnapshot.getChildren())的
公共无效onDataChange(DataSnapshot dataSnapshot){
$ ref $ addDistenerForSingleValueEvent(new ValueEventListenerBase(){
@Override
$ b System.out.println(userSnapshot.getKey()+:+ userSnapshot.getValue(User.class));
}
}
});

有了这个输出:

$ blockquote

brinchen:用户{name ='Brin Chen',friends = {mchen = true,hmadi = true},widgets = {one = true,three = true,two = true}}
$ b $ hmadi:User {name ='Horace Madi',friends = {brinchen = true},widgets = {one = true,two = true}}


mchen:用户{name ='Mary Chen',friends = {brinchen = true},widgets = {one = true,three = true}}


{
  "users": {
    "mchen": {
      "friends": { "brinchen": true },
      "name": "Mary Chen",
      "widgets": { "one": true, "three": true }
    },
    "brinchen": { ... },
    "hmadi": { ... }
  }
}

How to write custom object class for the above example? The guides in Firebase only show simple example.

解决方案

As usual, you need a Java class that maps each property from the JSON to a field+getter:

static class User {
    String name;
    Map<String, Boolean> friends;
    Map<String, Boolean> widgets;

    public User() { }

    public String getName() { return name; }
    public Map<String, Boolean> getFriends() { return friends; }
    public Map<String, Boolean> getWidgets() { return widgets; }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", friends=" + friends +
                ", widgets=" + widgets +
                '}';
    }
}

I really just follow these instructions from the Firebase guide on reading data on Android:

We'll create a Java class that represents a Blog Post. Like last time, we have to make sure that our field names match the names of the properties in the Firebase database and give the class a default, parameterless constructor.

Then you can load the information like this:

Firebase ref = new Firebase("https://stackoverflow.firebaseio.com/34882779/users");
ref.addListenerForSingleValueEvent(new ValueEventListenerBase() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
            System.out.println(userSnapshot.getKey()+": "+userSnapshot.getValue(User.class));
        }
    }
});

With this output:

brinchen: User{name='Brin Chen', friends={mchen=true, hmadi=true}, widgets={one=true, three=true, two=true}}

hmadi: User{name='Horace Madi', friends={brinchen=true}, widgets={one=true, two=true}}

mchen: User{name='Mary Chen', friends={brinchen=true}, widgets={one=true, three=true}}

这篇关于Firebase自定义Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆