[英]How to update the database value of item's quantity that is in the shopping cart?
I have an Android Shopping application that can be used to buy products based on availability of products in the inventory. 我有一个Android购物应用程序,可以根据库存中产品的可用性来购买产品。 At end I need to deduct the product quantity that is on the shopping cart and update the quantity of that item in database.
最后,我需要扣除购物车上的产品数量并更新数据库中该产品的数量。 The problem is that it currently updates the database value of quantity of the last item in the shopping cart.
问题是它当前更新购物车中最后一个项目的数量的数据库值。 How can I make it update all the database value of item's quantity that is in the shopping cart?.
如何使其更新购物车中商品数量的所有数据库值? Please someone help me in this.
请有人帮助我。 Bellow is the code for the class that does this.
贝娄是执行此操作的类的代码。
public class Thirdscreen extends Activity {
private static String url = "http://10.0.2.2/bootstrap-dist/postingdata.php";
// Progress Dialog
private ProgressDialog pDialog;
// Json parser object
JSONParser jsonParser = new JSONParser();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thirdscreen);
// final getQuantity getQan=new getQuantity();
TextView showCartContent = (TextView) findViewById(R.id.showCart);
TextView showCarttotal = (TextView) findViewById(R.id.showtotal);
Button btn = (Button) findViewById(R.id.subtractQuantity);
final Controller aController = (Controller) getApplicationContext();
int cartSize = aController.getCart().getCartSize();
String showString = "";
int total = 0;
for (int i = 0; i < cartSize; i++) {
final String pName = aController.getCart().getProducts(i)
.getProductName();
int pPrice = aController.getCart().getProducts(i).getProductPrice();
final int quantity = aController.getCart().getProducts(i)
.getProductQuantity();
final int dataProQuantityStore = aController.getCart()
.getProducts(i).getDatabaseProductQuantity();
String pDisc = aController.getCart().getProducts(i)
.getProductDesc();
total = total + (pPrice * quantity);
showString += "\n\nProduct Name : " + pName + "\n" + "Price : "
+ pPrice + "\t" + "Quantity: " + quantity + "\n"
+ "Discription : " + pDisc + ""
+ "\n -----------------------------------";
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int finalVal = dataProQuantityStore - quantity;
String finalstringValue=""+finalVal;
new getQuantity(pName,finalstringValue).execute();
}
});
}
showCarttotal.setText("Your Total is: " + total + "Tk");
showCartContent.setText(showString);
}
class getQuantity extends AsyncTask<String, String, String> {
public String productName;
public String productQuantity;
public getQuantity(String pn,String pq){
productName=pn;
productQuantity=pq;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Thirdscreen.this);
pDialog.setMessage("Processing you request..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// getting JSON Object
// Note that create product url accepts POST method
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", productName));
nameValuePairs.add(new BasicNameValuePair("pQuantity", productQuantity));
jsonParser.getandpostJSONFromUrl(url, "POST",nameValuePairs);
return null;
}
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
You are setting the OnClickListener
of your button repeatedly. 您正在重复设置按钮的
OnClickListener
。 However, a button has only one OnClickListener
. 但是,一个按钮只有一个
OnClickListener
。 You might need to implement a more elaborate non-anonymous OnClickListener
with an ArrayList
of getQuantity
objects. 您可能需要使用
getQuantity
对象的ArrayList
实现更复杂的非匿名OnClickListener
。 And the onClick()
method would iterate over the ArrayList
and execute()
every getQuantity
in it. 然后
onClick()
方法将遍历ArrayList
并execute()
每个getQuantity
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.