这是从我的数据库中的一个孩子读取长值的简单方法。
同样的方法也为pojo提供了null。
我做错了什么??? 为什么跳过OnDataChanged()?
(子路径:UserData / DeviceMgmt / Average :)
这是代码:
public class DevicesUtil {
public final static FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
public final static DatabaseReference dbRefUserData = firebaseDatabase.getReference("UserData");
public final static DatabaseReference dbRefUsers = dbRefUserData.child("Users");
public final static DatabaseReference dbRefDisplayNames = dbRefUserData.child("DisplayNames");
public final static DatabaseReference dbRefDeviceMgmt = dbRefUserData.child("DeviceMgmt");
public final static DatabaseReference dbRefAverage = dbRefDeviceMgmt.child("Average");
public final static DatabaseReference dbRefDevices = dbRefDeviceMgmt.child("Devices");
public final static DatabaseReference dbRefNumberOfAll = dbRefDeviceMgmt.child("NumberOfAll");
public final static DatabaseReference dbRefNumberOfSelected = dbRefDeviceMgmt.child("NumberOfSelected");
public final static DatabaseReference dbRefTotalDownloaded = dbRefDeviceMgmt.child("TotalDownloaded");
private static long theAverage;
public static long getAverage() {
dbRefAverage.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
theAverage = dataSnapshot.getValue(Long.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, databaseError.getMessage() + ", Exception: ", databaseError.toException());
}
});
return theAverage;
}
public static User getDbUser(final String uid) {
// getting User value:
dbRefUsers.child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange (DataSnapshot dataSnapshot) {
userValue = dataSnapshot.getValue(User.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, databaseError.getMessage() + ", Exception: ", databaseError.toException());
}
});
}}
和Json:
{
"的UserData" :{ " DeviceMgmt" :{ "平均" :666, "器件" :{ " -LEPhxstDLC-SJKITtCI" :{ "下载" :0, "故障" :0, " fcmToken" :" dRneV", " signOutTime" :0, "予以选取" :真的, " UID" :" AsU7GKtuTSZFwm3ePwvk4s3AEbT2", "使用" :0 }, "装置" :"虚拟" }, " NumberOfAll" :8, " NumberOfSelected" :8, " TotalDownloaded" :0 }, " DisplayNames" :{ " Pablo X" :" tWlLHuDgGVPr0ZivEEpIi4tik352", " Pablo XY" :" 0HW3pcWHU9YuPYM4Od4YBqDzB8C3", "的Pawel" :" AsU7GKtuTSZFwm3ePwvk4s3AEbT2", "显示名" :"虚拟" }, "用户" :{ " AsU7GKtuTSZFwm3ePwvk4s3AEbT2" :{ " deviceKey" :" -LEPhxstDLC-SJKITtCI", "显示名" :" Pawel", "电子邮件" :" email@gmail.com", "轮廓" :"用户", " signOutTime" :0 }, "用户" :"虚拟" } } }