Sou*_*rty 4 java android firebase firebase-realtime-database
运行此代码时:
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尽管数据库显示其中包含值,但该值仍然存在。
为了获取Hospital节点下的数据,需要更改以下引用:
hospitalDatabase = FirebaseDatabase.getInstance().getReference();
Run Code Online (Sandbox Code Playgroud)
和
hospitalDatabase = FirebaseDatabase.getInstance().getReference().child("Hospital");
Run Code Online (Sandbox Code Playgroud)
要获取方法内的数据getData(),请使用以下代码:
private void getData(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
String city = ds.child("city").getValue(String.class);
String name = ds.child("name").getValue(String.class);
}
}
Run Code Online (Sandbox Code Playgroud)
ds.getKey()将返回用户ID
ds.child("city").getValue(String.class)将返回城市。
ds.child("name").getValue(String.class)将返回名称。
| 归档时间: |
|
| 查看次数: |
8251 次 |
| 最近记录: |