我如何获得
如果快照不存在,则从此快照“在黑暗中跳舞”
:?我想它必须保存在某处的快照中。请阅读内联代码注释..
private void addListenerForSingleValueEvent(String streetAddress, StringBuilder targetAddress){
DatabaseReference firebase = FirebaseDatabase.getInstance().getReference();
firebase.child("catalog/trax").orderByChild("namn").equalTo("Dancing in the dark")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
// do sowm work on existing data
} else {
// How can I get the "Dancing in the dark" from the snapshot?
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Application.getInstance(), databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
答案 0 :(得分:0)
对Firebase数据库执行查询时,可能会有多个结果。因此快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。通过监听value
事件,您可以在一个快照中获得所有匹配的结果,因此您必须遍历这些子项。
DatabaseReference firebase = FirebaseDatabase.getInstance().getReference();
firebase.child("catalog/trax").orderByChild("namn").equalTo("Dancing in the dark")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot item: snapshot.getChildren()) {
// In this loop item is the snapshot of a single item.
// This means we can get the namm of the item
System.out.println(item.child("namm").getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Application.getInstance(), databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});