gpt4 book ai didi

android - 我怎么知道查询在 Firebase 中返回了 0 行?

转载 作者:行者123 更新时间:2023-11-29 15:09:35 25 4
gpt4 key购买 nike

从 firebase 示例中,我需要找出是否有 age=25 的恐龙,这可以按如下方式完成。但是说如果没有那个年龄的恐龙,我怎么知道查询结束了,有0个age=25的恐龙。因为我的 Android UI 依赖于此,所以才能进行下一步。

Firebase ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
Query queryRef = ref.orderByChild("height").equalTo(25);

queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChild) {
System.out.println(snapshot.getKey());
}
// ....
});

编辑提供的一些解决方案建议使用 ValueEventListener。但问题是即使你使用 valueEventListener,在上面的例子中,它仍然不起作用,因为有 0 行。 onDataChange 不会触发。

Firebase ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
Query queryRef = ref.orderByChild("height").equalTo(25);

queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChanged(DataSnapshot snapshot) {
System.out.println(snapshot.getKey());
}
// ....
});

回答

@Override
public void onDataChange(DataSnapshot snapshot) {
//DinosaurFacts facts = snapshot.getValue(DinosaurFacts.class);
//Log.d("hz-dino", facts.toString());

if(snapshot.getValue() != null)
{
Log.d("hz-dino", snapshot.getKey());
Log.d("hz-dino", String.valueOf(snapshot.getValue()));
}
else
{
Log.d("hz-dino", "there are exactly 0 rows!");
}
}

最佳答案

测试数据:

snapshot.getValue() != null

使用时

ref.addValueEventListener

如果该位置不存在数据,则快照将返回 null。

关于android - 我怎么知道查询在 Firebase 中返回了 0 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33085659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com