简体   繁体   中英

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. 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. However, a button has only one OnClickListener . You might need to implement a more elaborate non-anonymous OnClickListener with an ArrayList of getQuantity objects. And the onClick() method would iterate over the ArrayList and execute() every getQuantity in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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