简体   繁体   English

如何计算 android studio 中 firebase 的具体数值?

[英]How to count the specific values from firebase in android studio?

There are four seasons in the category, but I want to count only "Spring".该类别有四个季节,但我只想计算“春天”。 Finally, I want to print the counted number on the screen.最后,我想在屏幕上打印计数的数字。 What should I do?我应该怎么办?

enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

If I understood correctly, you want to get the number of children that exist under the "Products" node, which has the field productSeasonCategory set to "Spring".如果我理解正确,您想获取“Products”节点下存在的子节点数,该节点的productSeasonCategory字段设置为“Spring”。 If that's the case, then instead of using a database reference, you should use a query, as you can see in the following lines of code:如果是这种情况,那么您应该使用查询而不是使用数据库引用,如以下代码行所示:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference productsRef = db.child("Users").child(uid).child("Products");
Query queryByProductSeasonCategory = productsRef.orderByChild("productSeasonCategory").equalTo("Spring");
queryByProductSeasonCategory.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            long count = task.getResult().getChildrenCount();
            Log.d("TAG", "count: " + count);
            Text1.settext(String.valueOf(count)) //Added
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对 firebase android studio 中的多个值求和 - how to sum multiple values from firebase android studio 如何在android studio-java的firebase数据库中搜索得到子节点的具体数据? - How to get specific data of child node by searching from firebase database in android studio - java? Android Studio - 列出来自 Firebase 的评论 - Android Studio - listing reviews from Firebase Android Studio - 从 GetIdToken 获取 Firebase 令牌 - Android Studio - Get Firebase token from GetIdToken 如何重命名 firebase 存储中的文件夹 - Android studio - how rename folder in firebase storage - Android studio 使用 firebase 在 android 工作室中搜索 - search in android studio with firebase firebase 查询如何从子项中检索两个特定值 - firebase query how can i retrieve two specific values from child Google Data Studio:如何计算特定事件的数量 - Google Data Studio: How to count number of specific events 有什么方法可以从 android 工作室的代码中禁用 firebase 帐户 - is there is any way to disable firebase account from code in android studio 无法在 Android Studio 中从 Firebase 实时数据库检索数据 - Can't retrieve data from Firebase Realtime db in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM