如何计算Firebase实时数据库中子节点内的值的总和?

时间:2018-12-05 08:55:54

标签: android firebase firebase-realtime-database

这是我正在尝试的代码。我想将数据添加到子节点 结果 并显示这些值的总和:

 FirebaseDatabase firebaseDatabase1 = FirebaseDatabase.getInstance();
    DatabaseReference databaseReference1 = firebaseDatabase1.getReference("Sumofcal").child("result");

    ValueEventListener valueEventListener1 = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot1) {

            String itsme = String.valueOf(Objects.requireNonNull(dataSnapshot1.getValue()));

            Log.i("child", String.valueOf(itsme));

            int sum = 0;

            for (DataSnapshot ds : dataSnapshot1.getChildren()) {
                sum = Integer.parseInt(sum + itsme);
            }


        }


        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };
    databaseReference1.addValueEventListener(valueEventListener1);

这是logcat的输出:

 java.lang.NumberFormatException: For input string: "0{-LSxVZ3hsemUKAYOh0Gn= 95, -LSxVZnx5Bhv2C33_lk-= 10, -LSxVNYT0BJDytRaFXzb= 16}"
    at java.lang.Integer.parseInt(Integer.java:521)
    at java.lang.Integer.parseInt(Integer.java:556)
    at com.example.itsandpro.Signup.Api.SearchAdapter$2.onDataChange(SearchAdapter.java:135)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.5:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.5:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.5:55)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我有这样的实时数据库设置。我想将pushId与数字分开,然后对所有值进行加峰处理。

    Sumofcal
result
-LSxVNYT0BJDytRaFXzb: 
" 16"
-LSxVZ3hsemUKAYOh0Gn: 
" 95"
-LSxVZnx5Bhv2C33_lk-: 
" 10"

1 个答案:

答案 0 :(得分:0)

要解决此问题,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference resultRef = rootRef.child("Sumofcal").child("result");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int total = 0;
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            int number = Integer.valueOf(ds.getValue(String.class).replaceAll(" ", ""));
            total = total + number;
        }
        Log.d(TAG, "total= " + total);
    }

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

您的logcat中的结果将是:

total= 121

顺便说一句,我强烈建议您在创建总和时使用数字而不使用字符串。另外,请注意您的值中存在的空间:

    " 16"
//   ^