简体   繁体   English

如何更新购物车中商品数量的数据库值?

[英]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()方法将遍历ArrayListexecute()每个getQuantity

暂无
暂无

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

相关问题 android如何在购物车中添加数量? - android How to add quantity in the shopping cart? android:如何增加购物车中物品的数量 - android: how to increase number of quantity of an item in the cart Android Studio Java:如果产品已经存在于我的购物车活动中,我该如何更新我的数量? - Android Studio Java: How can I update my quantity if the product already exist's in my Cart Activity? Spring MVC Hibernate中刷新页面时如何添加更多购物车而不增加数量 - How to add more shopping cart not increment quantity when refresh page in spring mvc hibernate 如何在数据库中存储不同尺寸的物品数量 - How to store the quantity of item for different sizes in the database 通过增加数量值而不是将商品添加到新行来将相同商品添加到购物车 - Add same item to cart by increasing the quantity value instead of adding the item to a new row 无法从购物车中移除商品 - Cannot remove item from shopping cart 用户刷新页面时,如何防止购物车商品增加? - How can I prevent the shopping cart item from incrementing when the user refresh the page? 如何使用并发用户处理乐观锁定项目数量更新? - How to handle Optimistic Locked item quantity update with concurrent users? 如何点击购物车页面中的“更新”按钮-Url =“ http://live.guru99.com/” - How to make click on the Update Button in the shopping Cart Page - Url=“http://live.guru99.com/”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM