【问题标题】:Calculate total from values stored in Firebase Firestore Database-Android根据存储在 Firebase Firestore Database-Android 中的值计算总计
【发布时间】:2022-10-23 14:20:05
【问题描述】:

我正在尝试从云 Firestore 数据库中获取集合字段的总值。不是成功。请帮我。

DocumentReference productsRef = firebaseFirestore
        .collection("report").document(userID)
        .collection("year").document(userID2)
        .collection("month").document(userID3)
        .collection("report").document();

productsRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            double total = 0;
            for (QueryDocumentSnapshot document : task.getResult()) {
                double itemCost = document.getDouble("total");
                total += itemCost;
            }
           // Log.d("TAG", String.valueOf(total));
            dtotal.setText(String.valueOf(total));
        }
    }
});

【问题讨论】:

  • “不成功”真的很难帮助。当您在共享代码的每一行上设置断点时,在调试器中运行,然后检查每一行上每个变量的值:第一行没有按照您的预期执行的操作是什么?

标签: java firebase google-cloud-firestore


【解决方案1】:

很高兴知道现在我已经自己解决了......

    DocumentReference productsRef = firebaseFirestore.collection("report").document(userID)
            .collection("year").document(userID2)
            .collection("month").document(userID3);

    Query query = productsRef.collection("report");

    query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                int sum = 0;
                for (QueryDocumentSnapshot document : task.getResult()) {
                    String itemCost = document.getString("total");
                    int total = Integer.parseInt(itemCost);
                    sum = sum + total;
                    dtotal.setText(String.valueOf(sum));


                }


            }
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2014-02-08
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多