gpt4 book ai didi

android - 在 Android 上过滤 Firebase 数据库 DataSnapshot 子级

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:37 24 4
gpt4 key购买 nike

我想在 Android 上查询来自 Firebase 的数据。这是我的数据 my data

这是我的 Android Studio 代码:

Firebase myFirebaseRef = new Firebase("https://....../myCoffee/"+phone+"");
myFirebaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
int size= (int) snapshot.getChildrenCount();
txtMyCoffee.setText(""+size);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});

问题: child 的总数是 4。我如何过滤 child 以只计算“coffeeNo”为“Acoffee”的 child ?

最佳答案

Just improve your query to get only the result your are looking for:

DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://....../myCoffee/"+phone+"");
Query acoffeeQuery = databaseRef.orderByChild("coffeeNo").equalTo("Acoffee");
acoffeeQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
final int size = (int) snapshot.getChildrenCount();
//size is equal to 2
}

@Override
public void onCancelled(FirebaseError firebaseError) {}
});

This way, you do not have to loop over all results and you do not haveto implement a counter manually.

关于android - 在 Android 上过滤 Firebase 数据库 DataSnapshot 子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44927873/

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