Bjø*_*gen 8 android firebase firebase-realtime-database
如何确定Firebase数据库节点是否没有子节点?似乎获取数据的唯一方法是使用侦听器,并且只有在添加或删除某些内容时才会触发.换句话说,如果没有孩子,什么都不会发生.
您可以使用addValueEventListener,并且onDataChange您将有一些方法来检查没有孩子。
将会addValueEventListener起作用,因为对这个文档进行了编码
当附加侦听器时会触发此方法一次,每次数据(包括子项)发生更改时都会触发此方法
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
// As cricket_007 we also can use hasChildren or getChildrenCount
if(!snapshot.hasChildren()){
// db has no children
}
// OR this way
if(snapshot.getChildrenCount() == 0){
// db has no children
}
// OR this way
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
// db has no children
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
766 次 |
| 最近记录: |