teamapp-25ba7
schedules
-LHc3zKZhNFLq536dpA1
UID:
date:
details:
time:
title:
-LHc7MBAoNwLWCNgkZ_y
UID:
date:
details:
time:
title:
从上面的节点示例中,我只想获取详细信息和时间值,我的问题是如何只检索这两个值。我尝试遍历所有值,但这让我得到了一切
答案 0 :(得分:1)
假设您要从所有对象中获取details
和time
属性的值,并假设details
属性的类型为String,而time
为 ServerValue.TIMESTAMP ,要解决此问题,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference schedulesRef = rootRef.child("schedules");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String details = ds.child("details").getValue(String.class);
long time = ds.child("time").getValue(Long.class);
Log.d("TAG", details + " / " + time);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
};
schedulesRef.addListenerForSingleValueEvent(valueEventListener);