运行此代码时:
private void getData(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
Log.d(TAG, "UserID inside getData: "+userID);
Log.d(TAG, "User Name inside getData: "+ds.child(userID).child("name").getValue());
Log.d(TAG, "DS inside getData: "+ds.child(userID));
hospitalCity = String.valueOf(ds.child(userID).child("city").getValue());
Log.d(TAG, "User city inside getData: "+ds.child(userID).child("city").getValue());
break;
}
}
Run Code Online (Sandbox Code Playgroud)
日志显示了这一点:
getData 内的用户 ID:Lsncj8CIsfTQXc7E425AtLuDI5v2
getData 内的用户名:null getData 内的 DS:DataSnapshot { key = Lsncj8CIsfTQXc7E425AtLuDI5v2,value = null }D/DonorList:getData 内的用户城市:null
这是数据库:
正如您所看到的,它获取了键,但是null尽管数据库显示其中包含值,但该值仍然存在。