簡體   English   中英

如何更新購物車中商品數量的數據庫值?

[英]How to update the database value of item's quantity that is in the shopping cart?

我有一個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();

    }

}

您正在重復設置按鈕的OnClickListener 但是,一個按鈕只有一個OnClickListener 您可能需要使用getQuantity對象的ArrayList實現更復雜的非匿名OnClickListener 然后onClick()方法將遍歷ArrayListexecute()每個getQuantity

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM