如何在不知道firebase实时数据库中的父元素的情况下访问子元素?如何在不知道父级的情况下通过 userId 获取数据。
这是我的数据库结构。
假设该following
节点是 Firebase 根的直接子节点,要显示followCase
、parentId
和userId
属性的值,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference followingRef = rootRef.child("following");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
for(DataSnapshot dSnapshot : ds.getChildren()) {
String followCase = ds.child("followCase").getValue(String.class);
String parentId = ds.child("parentId").getValue(String.class);
String userId = ds.child("userId").getValue(String.class);
Log.d("TAG", followCase + " / " + parentId + " / " + userId);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
followingRef.addListenerForSingleValueEvent(valueEventListener);
输出将是:
0 / B4RV...NI73 / P3y8...8NO2
//and so on
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句