Skip to content
Advertisement

I want cumulative of production qty which I saved in key value ‘pqty’ in firebase?

I want cumulative of production qty in key ‘pqty’ property:

JavaScript

I tried this and got output as 0. But I want output as 750.

I’m using sketch ware.

enter image description here

Advertisement

Answer

First of all, you are storing the “pqty” property in your database as a String and not as a number, which in my opinion is a really bad idea. If your data type is a number, then you should store it like that. Now, to get the desired sum, please use the following lines of code:

JavaScript

According to your screenshot, the result in your logcat will be:

JavaScript

Things to notice:

  • There’s no need for any “.child(“pqty”)” call in your “valuesRef” reference whatsoever, because you need to loop through the “DataSnapshot” object using a call to “.getChildren()” method and then get the value of “pqty” property.
  • To be able to sum those values, you need to parse that String object into a number using Integer.parseInt(String s) method.
  • There is no way you can get a String value from the database using “.getValue(Integer.class)”, as it will always yield an Exception. You need to get it as it’s actually stored.
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement