简体   繁体   English

回路数量值的总和?

[英]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.. 我想对数量p.getString("customers_basket_quantity")求和并设置为我的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 并将qtySum设置为我的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. 在当前的实现中,由于TextView具有重载的setText(int resId)方法,因此您尝试为TextView设置资源ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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