简体   繁体   English

我如何在 firebase 数据库中使用相同的键来总结值

[英]How do i sum up values with the same key in firebase database

I'm creating a shopping app where users add items to cart and then I store these values in the Firebase database.我正在创建一个购物应用程序,用户可以在其中将商品添加到购物车,然后我将这些值存储在 Firebase 数据库中。 The problem I've is trying to get the total amount of product added to the cart.我遇到的问题是试图获取添加到购物车的产品总量。 I have a MapChild with key productPrice , now how do I sum up all the data with key productPrice after saving them in a listmap.我有一个带有键productPrice的 MapChild,现在如何在将所有数据保存在列表图中后用键productPrice总结所有数据。

这是我的 firebase 数据库的屏幕截图

To sum all the values of the productPrice property, please use the following lines of code:要对productPrice属性的所有值求和,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference cartItemsRef = rootRef.child("cartItems");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        long total = 0;
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String productPrice = ds.child("productPrice").getValue(String.class);
            total += Long.parseLong(productPrice);
        }
        Log.d("TAG", "total: " + total);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
    }
};
cartItemsRef.addListenerForSingleValueEvent(valueEventListener);

The result in the logcat will be: logcat 中的结果将是:

total: 730

If you need to store prices, it's best to store them as numbers and not as String values, as you do right now.如果您需要存储价格,最好将它们存储为数字而不是字符串值,就像您现在所做的那样。 If you intend to change that, then you should simply use:如果你打算改变它,那么你应该简单地使用:

total += productPrice;

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

相关问题 我如何消除实时数据库中自动生成的密钥 firebase flutter - How do i eliminate the autogenerated key in realtime database firebase flutter 如何解决 Key 中的 FireBase 数据库禁用字符或解决方法 - How can I solve FireBase database forbidden characters in Key or a workaround 如何删除 firebase 数据库中的整个关键数据? - How to delete whole key data in firebase database? 如何在 firebase 上保存相同 JS 代码的多个实例? - How do I save multiple instances of the same JS code on firebase? 如何管理连接到同一项目的 2 个 Firebase 应用程序 - How do I manage 2 Firebase apps connecting to the same project 如何更新实时数据库中的键值? - How can I update key values in realtime database? 如何从 Firebase 数据库中获取密钥? - How to get key from Firebase Database? 如何同时运行 getDownloadURL 和 getMetaData 并将结果输入同一个 object? Firebase 存储 - How do I run both getDownloadURL and getMetaData at once and get the results into the same object? Firebase Storage Firebase 实时数据库,我怎么知道数据集何时完成加载 - Firebase Realtime Database, how do I know when dataset has finished loading 如何将一个值仅推送一次到 firebase 数据库 (firebase-admin)? - How do I push a value only once into firebase database (firebase-admin)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM