读取从Firebase的ChildEventListener()获取的DatasnapShot的内容的问题

时间:2017-05-06 19:45:03

标签: android firebase firebase-realtime-database

我无法从dataSnapshot中提取字段值。

以下是代码:

ChildEventListener userListener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        Log.d(TAG,"users count:"+dataSnapshot.getChildrenCount()+" users");

        for(DataSnapshot receivedSnapshot:dataSnapshot.getChildren()) {
            Log.d(TAG,"received Snapshot"+receivedSnapshot);
            Toast.makeText(getBaseContext(),"received Snapshot"+receivedSnapshot,Toast.LENGTH_LONG).show();
            Person person  = receivedSnapshot.getValue(Person.class)// Getting the error for this line.

            Log.i(TAG,"receivedSnapshot:"+person.toString());

        }
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
};

dataBase =  FirebaseDatabase.getInstance();
DatabaseReference userTable = dataBase.getReference("users");
userTable.addChildEventListener(userListener);

这是logcat:

Process: com.example.raghavkishan.wealthmanagement, PID: 23843
Theme: themes:{default=overlay:system, iconPack:website.leifs.delta, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.raghavkishan.wealthmanagement.Person

我的人员类:

private String personName;
private String personEmail;
private int phoneNumber;
private String dateOfBirth;
private String city;
private String state;
private String country;
private String groupId;

public Person(String personName,String personEmail,int phoneNumber,String dateOfBirth,String city,String state,String country,String groupId){
    this.personName = personName;
    this.personEmail = personEmail;
    this.phoneNumber = phoneNumber;
    this.dateOfBirth = dateOfBirth;
    this.city = city;
    this.state = state;
    this.country = country;
    this.groupId = groupId;

}[![enter image description here][1]][1]

图像包含数据库结构。用户的UID是节点的关键。

enter image description here

我正在使用Person类的对象将数据发布到firebase数据库。但在检索它时,我遇到了上述错误。

1)我犯了什么错误?

2)任何替代方法?

0 个答案:

没有答案