简体   繁体   中英

sum for loop quantity values?

i have this code..

JSONObject jsonObjcart = new JSONObject(myJSONCartProducts);
jsonarrayCartProducts = jsonObjcart.getJSONArray("cartproducts");

cartarraylist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jsonarrayCartProducts.length(); i++) {
    HashMap<String, String> lmap = new HashMap<String, String>();
    JSONObject p = jsonarrayCartProducts.getJSONObject(i);
    // Retrive JSON Objects
    lmap.put("products_id", p.getString("products_id"));
    lmap.put("products_name", p.getString("products_name"));
    lmap.put("products_price", p.getString("products_price"));
    lmap.put("products_image", p.getString("products_image"));
    lmap.put("customers_basket_quantity", p.getString("customers_basket_quantity"));
    lmap.put("products_price_total", p.getString("products_price_total"));
    lmap.put("pcustomersid", customersid);
    lmap.put("pcountryid", countryid);
    lmap.put("customers_basket_id", p.getString("customers_basket_id"));
    // Set the JSON Objects into the array
    cartarraylist.add(lmap);
}

i want to sum the quantity p.getString("customers_basket_quantity") and set to my textview..

i tried to do create an

int qtySum=0;
int qtyNum;

and do this inside for loop..

qtyNum = Integer.parseInt(p.getString("customers_basket_quantity"));
        qtySum += qtyNum;

and set qtySum to my textview

textTotalitems.setText(qtySum);

but i got error, the app crashed..

this is updated code with sum i tried..

JSONObject jsonObjcart = new JSONObject(myJSONCartProducts);
jsonarrayCartProducts = jsonObjcart.getJSONArray("cartproducts");

cartarraylist = new ArrayList<HashMap<String, String>>();
int qtySum=0;
int qtyNum;
for (int i = 0; i < jsonarrayCartProducts.length(); i++) {
    HashMap<String, String> lmap = new HashMap<String, String>();
    JSONObject p = jsonarrayCartProducts.getJSONObject(i);
    // Retrive JSON Objects
    lmap.put("products_id", p.getString("products_id"));
    lmap.put("products_name", p.getString("products_name"));
    lmap.put("products_price", p.getString("products_price"));
    lmap.put("products_image", p.getString("products_image"));
    lmap.put("customers_basket_quantity", p.getString("customers_basket_quantity"));
    lmap.put("products_price_total", p.getString("products_price_total"));
    lmap.put("pcustomersid", customersid);
    lmap.put("pcountryid", countryid);
    lmap.put("customers_basket_id", p.getString("customers_basket_id"));
    // Set the JSON Objects into the array
    qtyNum = Integer.parseInt(p.getString("customers_basket_quantity"));
    qtySum += qtyNum;
    cartarraylist.add(lmap);
}
textTotalitems.setText(qtySum);

Use

textTotalitems.setText(String.valueOf(qtySum));

instead of

textTotalitems.setText(qtySum);

With your current implementation your trying to set a resource-Id to your TextView, because TextView has an overloaded setText(int resId)-method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM