将DataSnapshot转换为类对象

时间:2017-11-23 14:13:40

标签: java android firebase firebase-realtime-database

我是Firebase数据库的新手,我正在关注Google的文档以从我的数据库中检索数据。
我有这个结构:

root/
|
|--- 82JWZZZd***
|    |
|    |--- profile
|         |
|         |--- id: 1
|         |--- name: Arnold Schwarzenegger
|         |--- age: 70
|
|--- GB3FDPiw***
     |
     |--- profile
          |
          |--- id: 1
          |--- name: Sylvester Stallone 
          |--- age: 71

我想要检索用户的配置文件信息,我有一个Java类:

  render: function() {

    var series = this.series,
        yAxis = this.yAxis[0],
      stacks = yAxis.stacks;

    for (var key in stacks) {
      var stack = stacks[key];
                series.forEach(function(s) {
        var stack_;
        for(var key_ in stack) {        
            stack_ = stack[key_];
        }

        if(s.stackKey.substring(6) === stack_.stack) {
            stack_.label.attr({
            x: s.points[0].plotX
          });
        }
      });
    }
  }

所以,正如我从谷歌的文档中学到的,我创建了一个监听器:

public class Profile{
    private int id;
    private String name;
    private int age;

    public Profile(){
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public Profile(int id, String name, int age){
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

Toast告诉我这个:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference(getUid() + "/profile"); reference.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()){ Toast.makeText(getContext(), "" + dataSnapshot.getValue(), Toast.LENGTH_LONG).show(); // Profile prf = dataSnapshot.getValue(Profile.class); } } @Override public void onCancelled(DatabaseError databaseError) { } });

但是如果我取消注释该行

{id=1, name=Arnold Schwarzenegger, age=71}

我的应用 CRASH

我不明白与Google文档的区别。有人能帮助我吗?我太过于挣扎了!

错误是:

com.google.firebase.database.DatabaseException: Class com.companyname.myApp.FirstFragment$Profile is missing a constructor with no arguments

2 个答案:

答案 0 :(得分:3)

如果您有私人字段,则需要添加getter和setter。如果您不想要制定者和吸气者,请使用公共字段。

此外,由于您的Profile类嵌套在片段中,因此您需要将其标记为static。所以public static class Profile{。另请参阅关于Github的评论:https://github.com/firebase/FirebaseUI-Android/issues/46#issuecomment-167373575

答案 1 :(得分:0)

DatabaseReference reference = FirebaseDatabase.getInstance().getReference(getUid() + "/profile");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()){
            //Create new collection<Profile> person;

            person=new ArrayList<Profile>();
            Toast.makeText(getContext(), "" + dataSnapshot.getValue(),
                    Toast.LENGTH_LONG).show();
            Map<String,String> td=(HashMap<String, String>)dataSnapshot.getValue();
            Profile prf =new Profile(td.get("id"),td.get("name"),td.get("age"))

            person.add(prf);

        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});