-1
這是在我的StringRequest的Response.Listener中。如何從我的JSON數組中獲得列的總和
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("cart");
for (int i = 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();
}
}
它會得到JSON數組,並把它放在我的CartItem.java,將填充我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;
我如何總數組中所有的價格是多少?
您確切的問題是什麼?你不知道如何循環訪問數組?你不知道如何訪問'CarItem'中的成員變量?你不知道如何將一個字符串轉換爲數字?你不知道如何添加數字? –