, , . , , .
- 1:
Serializable , , . / .
public class Product implements Serializable {
public String productName;
public int price;
public int quantity;
public int total;
}
- 2: , , UserProducts ArrayList, gson, link1 link2.
- 3: total = price * quantity listview setOnItemClickListener, ,
Product product = userProducts.get(postiton);
product.total = product.price * product.quantity;
- 4: arraylist Serializable ,
Intent intent = new Intent(ProductActivity.this, BillingActivity.class);
intent.putExtra("user_products", userProducts);
startActivity(intent);
- 5: ,
if (getIntent() != null) {
userProducts = (ArrayList<Product>) getIntent()
.getSerializableExtra("user_products");
}
- 6: , ? , jsonarray jsonobject , jsonobject , .
try {
JSONObject mainJObject = new JSONObject();
JSONArray productJArray = new JSONArray();
for (int i = 0; i < userProducts.size(); i++) {
JSONObject productJObject = new JSONObject();
productJObject.put("productname", userProducts.get(i).productName);
productJObject.put("price", userProducts.get(i).price);
productJObject.put("quantity", userProducts.get(i).quantity);
productJObject.put("total", userProducts.get(i).total);
productJArray.put(productJObject);
}
mainJObject.put("products", productJArray);
mainJObject.put("grandd_total", grandTotal);
} catch (Exception e) {
e.printStackTrace();
}
:
{
"products": [
{
"productname": "p1",
"price": "15",
"quantity": "6",
"total": 90
},
{
"productname": "p2",
"price": "25",
"quantity": "4",
"total": 100
}
],
"grandd_total": 190
}