Home > Net >  Calculate Sum of values from node Firebase
Calculate Sum of values from node Firebase

Time:12-31

enter image description here

I am getting all quantity price. But I don't know how to add them all and get it under one string. Can someone pls help

FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance();

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference databaseReference = mFirebaseDatabase.getReference().child("cart").child(uid);

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
            Log.d("dasdasdasdad", String.valueOf(dataSnapshot));

            String quantityprice = (String) dataSnapshot.child("quantityprice").getValue();

            Log.d("quantityprice", String.valueOf(quantityprice));
        }
    }

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

    }
});

In here i am getting all quantityprice value in Log i.e:- 130,55,150

But i dont know how to add them all and get it under one string. Anyhelp would be appreciated.

I have tried this too

DatabaseReference databaseReference = mFirebaseDatabase.getReference().child("cart").child(uid);

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
                    Log.d("dasdasdasdad", String.valueOf(dataSnapshot));

                    Integer total = 0;

                    String quantityprice = (String) dataSnapshot.child("quantityprice").getValue();

                    Log.d("quantityprice", String.valueOf(quantityprice));

                    int price = Integer.valueOf(quantityprice);

                    total =  price;
                Log.d("total",total);   }

but total is shown different all time.

This is Log

D/quantityprice: 130 D/total: 130

D/quantityprice: 55 D/total: 55

D/quantityprice: 150 D/total: 150

CodePudding user response:

To solve this problem, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference productsRef = db.child("card").child(uid);
productsRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            double total = 0.00;
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String quantityPrice = ds.child("quantityprice").getValue(String.class);
                total  = Double.parseDouble(quantityPrice);
            }
            Log.d("TAG", "total: "   total);
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The result in the logcat will be:

total: 335.00

The key to solving this problem is to parse the String value from the database to a double. Remember, if you need numbers, it's best to store them in the database as numbers and not as String values.

  • Related