0
我無法從dataSnapshot中提取字段值。讀取從Firebase的ChildEventListener()獲取的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
我的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是節點的關鍵。
我使用Person類的一個對象把數據上傳到數據庫的火力點。但在檢索它時,我遇到了上述錯誤。
1)我犯了什麼錯誤?
2)任何替代方法?
請發佈Person類定義和firebase節點 –
請添加您的數據庫結構和您的pojo。 –
@SushobhNadiger我已更新人員類和數據庫結構 –