1. Check out our companion app, Forums for Android! Download from Google Play

maximun total quantity 5 is add to cart in android

Discussion in 'Application Development' started by krishnaveni, Jan 3, 2013.

  1. krishnaveni

    krishnaveni Well-Known Member
    Thread Starter
    55

    Dec 16, 2011
    158
    0
    55
    chennai
    I have developed one android application.

    Here i have to add the maximum quantity is 5 to cart.

    But i have wrote the condition is correct for these requirement.

    I have to run the application means more product is added to cart.please help me.

    please check my code and give me solution for these.

    whats wrong in my code.please explain me.

    [HIGH]
    public class SingleMenuItem extends Activity {
    static final String KEY_PNAME = "productName";
    static final String KEY_PRICE = "productPrice";
    static final String KEY_TOTAL = "total";
    static final String KEY_QTY = "qty";
    static final String KEY_THUMB_URL = "productImage";
    private EditText edit_qty_code;
    private TextView txt_total;
    private TextView text_cost_code;
    private double itemamount = 0;
    private double itemquantity = 0;
    String mTitle, mQty, mCost, mTotal;
    int total_count;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single);
    Button back = (Button) findViewById(R.id.back);
    back.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    Intent intent = new Intent();
    finish();
    }
    });
    Intent in = getIntent();
    String title = in.getStringExtra(KEY_PNAME);
    String thumb_url = in.getStringExtra(KEY_THUMB_URL);
    String cost = in.getStringExtra(KEY_PRICE);
    String total = in.getStringExtra(KEY_TOTAL);

    ImageLoader imageLoader = new ImageLoader(getApplicationContext());

    ImageView imgv = (ImageView) findViewById(R.id.single_thumb);
    final TextView txttitle = (TextView) findViewById(R.id.single_title);
    TextView txtheader = (TextView) findViewById(R.id.actionbar);
    text_cost_code = (TextView) findViewById(R.id.single_cost);
    edit_qty_code = (EditText) findViewById(R.id.single_qty);
    edit_qty_code.setText("1");
    txt_total = (TextView) findViewById(R.id.single_total);

    txttitle.setText(title);
    txtheader.setText(title);
    text_cost_code.setText(cost);
    txt_total.setText(total);
    imageLoader.DisplayImage(thumb_url, imgv);

    itemamount = Double.parseDouble(text_cost_code.getText().toString());

    txt_total.setText(Double.toString(itemamount));

    edit_qty_code.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before,
    int count) {
    // TODO Auto-generated method stub
    if (!edit_qty_code.getText().toString().equals("")
    || !edit_qty_code.getText().toString().equals("")) {
    itemquantity = Double.parseDouble(edit_qty_code.getText()
    .toString());
    itemamount = Double.parseDouble(text_cost_code.getText()
    .toString());
    txt_total.setText(Double
    .toString(itemquantity * itemamount));
    } else {
    txt_total.setText("0.00");
    }

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {
    // TODO Auto-generated method stub

    }

    public void afterTextChanged(Editable s) {

    }
    });

    ImageButton mImgAddCart = (ImageButton) findViewById(R.id.img_add);
    mImgAddCart.setOnClickListener(new OnClickListener() {



    public void onClick(View v) {
    // TODO Auto-generated method stub
    mTitle = txttitle.getText().toString();
    mCost = text_cost_code.getText().toString();
    mCost = mCost.replace("From ", "");
    mTotal = txt_total.getText().toString();
    mTotal = mTotal.replace("From ", "");
    mQty = edit_qty_code.getText().toString();

    if ( (Constants.mItem_Detail.size() <= 0)) {
    HashMap<String, String> mTempObj = new HashMap<String, String>();

    mTempObj.put(KEY_PNAME, mTitle);

    mTempObj.put(KEY_QTY, mQty);

    mTempObj.put(KEY_PRICE, mCost);

    mTempObj.put(KEY_TOTAL, mTotal);

    Constants.mItem_Detail.add(mTempObj);
    total_count=total_count+Integer.parseInt(mQty);

    }

    else {
    for (int i = 0; i < Constants.mItem_Detail.size(); i++) {
    if (Constants.mItem_Detail.get(i).get(KEY_PNAME)
    .equals(mTitle)) {
    Constants.mItem_Detail.remove(i);
    break;

    } else {

    if (total_count <= 5) {

    HashMap<String, String> mTempObj = new HashMap<String, String>();



    mTempObj.put(KEY_PNAME, mTitle);



    mTempObj.put(KEY_QTY, mQty);



    mTempObj.put(KEY_PRICE, mCost);



    mTempObj.put(KEY_TOTAL, mTotal);







    Constants.mItem_Detail.add(mTempObj);
    total_count=total_count+Integer.parseInt(mQty);



    }
    }
    }

    HashMap<String, String> mTempObj = new HashMap<String, String>();
    mTempObj.put(KEY_PNAME, mTitle);
    mTempObj.put(KEY_QTY, mQty);
    mTempObj.put(KEY_PRICE, mCost);
    mTempObj.put(KEY_TOTAL, mTotal);

    Constants.mItem_Detail.add(mTempObj);
    }

    AlertDialog.Builder alertdialog = new AlertDialog.Builder(
    SingleMenuItem.this);
    alertdialog.setTitle(getResources()
    .getString(R.string.app_name));
    alertdialog.setMessage("Add in ViewCart");

    alertdialog.setPositiveButton("OK",
    new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog,
    int which) {
    // TODO Auto-generated method stub
    //finish();
    return;
    }
    });

    alertdialog.show();

    }
    });

    ImageButton mImgViewCart = (ImageButton) findViewById(R.id.img_view);
    mImgViewCart.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub

    Intent mInViewCart = new Intent(SingleMenuItem.this,
    ViewCartActivity.class);
    mInViewCart.putExtra("Title", mTitle);
    startActivity(mInViewCart);
    }
    });
    [/HIGH]
     

    Advertisement

  2. BroncoDroid

    BroncoDroid Active Member
    36

    May 7, 2012
    30
    4
    36
    So after you restart the app, its letting you put 5+ items in your cart?
     
  3. krishnaveni

    krishnaveni Well-Known Member
    Thread Starter
    55

    Dec 16, 2011
    158
    0
    55
    chennai
    yes.here i have to maximum quantity is 5 add to cart in android application.but i have to run the application with above code means more product is added to cart.how can i write the condition for add maximum 5 quantity is add to cart in these android application.please help me.
     
  4. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    because your totalCount member is an instance variable. Make it a static variable and it should work.
     

Share This Page

Loading...