java - How can I get the sum of a column from my JSON Array -


this inside response.listener on stringrequest.

                try {                         jsonobject jsonobject = new jsonobject(response);                         jsonarray array = jsonobject.getjsonarray("cart");                         (int = 0; i<array.length(); i++){                             jsonobject o = array.getjsonobject(i);                             cartitem item = new cartitem(                                     o.getstring("cardno"),                                     o.getstring("product_id"),                                     o.getstring("name"),                                     o.getstring("quantity"),                                     o.getstring("price"),                                     o.getstring("category")                             );                             cartitems.add(item);                         }                         adapter = new cartadaptor(cartitems, getcontext());                         recyclerview.setadapter(adapter);                      } catch (jsonexception e) {                         e.printstacktrace();                     }                 } 

it json array , put in cartitem.java , populate cartadaptor.java

public cartitem(string cardno, string product_id, string name, string quantity, string price, string category) {     this.cardno = cardno;     this.product_id = product_id;     this.name = name;     this.quantity = quantity;     this.price = price;     this.category = category; 

how can total price in array?

i'm not sure want total price.

and suggest one.

            try {                     int total = 0;                         //  add                      jsonobject jsonobject = new jsonobject(response);                     jsonarray array = jsonobject.getjsonarray("cart");                     (int = 0; i<array.length(); i++){                         jsonobject o = array.getjsonobject(i);                         cartitem item = new cartitem(                                 o.getstring("cardno"),                                 o.getstring("product_id"),                                 o.getstring("name"),                                 o.getstring("quantity"),                                 o.getstring("price"),                                 o.getstring("category")                         );                         cartitems.add(item);                          //  add                         if(o.getstring("price") != null &&                            o.getstring("price") != "" ){                             total += integer.parseint(o.getstring("price"));                         }                       }                     adapter = new cartadaptor(cartitems, getcontext());                     recyclerview.setadapter(adapter);                  } catch (jsonexception e) {                     e.printstacktrace();                 }             } 

then can total prices.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -