Firebase:datasnapshot.getValue(className)不起作用

时间:2016-11-30 14:11:42

标签: android firebase firebase-realtime-database

我是Firebase的新手。我尝试使用dataSnapshotgetValue(className)获取数据。但无论我尝试对象返回null

我的课程和目录如下

型号:packageName/Models/TongTin.class

活动:packageName/Activities/MainActivity.class

这是我的模型代码:

@IgnoreExtraProperties
public class TongTin {

private String type;
private String lotPrice;
private String note;
private String user_uid;
private DatabaseReference databaseReference;

public TongTin() {

}

public TongTin(String type, String lotPrice, String note, String user_uid) {
    databaseReference = FirebaseDatabase.getInstance().getReference();
    this.type = type;
    this.lotPrice = lotPrice;
    this.note = note;
    this.user_uid = user_uid;
}

public String getUserUid() {
    return user_uid;
}

public String getType() {
    return type;
}

public String getNote() {
    return note;
}

public void addTongTin() {
    String key = databaseReference.child(TongTinTable.tableName).push().getKey();
    Map<String,Object> postValues=toMap();
    Map<String,Object> childUpdates=new HashMap<>();
    childUpdates.put(TongTinTable.postURL+key,postValues);
    databaseReference.updateChildren(childUpdates);
}

}

这里onDataChange():

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    TongTin tongTin = dataSnapshot.getValue(TongTin.class);
    Log.d("Tong Tin",tongTin.getNote());
}

1 个答案:

答案 0 :(得分:3)

你可能会收回你的收藏品......如果是这样你就需要做一些像

这样的事情
for (DataSnapshot childSnapshot: dataSnapshot.getChildren())