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

时间:2014-06-16 10:58:34

标签: java android

我有一个Android购物应用程序,可用于根据库存中的产品可用性购买产品。最后,我需要扣除购物车上的产品数量并更新数据库中该项目的数量。问题是它当前更新了购物车中最后一项数量的数据库值。如何更新购物车中商品数量的所有数据库值?请有人帮帮我。贝娄是这个类的代码。

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();

    }

}

1 个答案:

答案 0 :(得分:0)

您正在重复设置按钮的OnClickListener。但是,按钮只有一个OnClickListener。您可能需要使用OnClickListener ArrayListgetQuantity对象来实现更精细的非匿名onClick()ArrayList方法会迭代execute()getQuantity中的每个{{1}}。