Tony Stark Tony Stark - 5 months ago 53
Android Question

add item to cart without popping "order quantity" button

Need Help,
I am customising an eCommerce app .
Problem: When I select item n press buy button (id=btnadd), A dialogue box pops up asking me to enter order quantity. if I enter 1 and press add, it adds 1 unit to the cart. I don't want to enter quantity at all.It should add a unit of that item and go to cart page.

What I want to do: Press buy button n article should get added to cart and page should also go to cart page. I don't want order quantity popup. By default a single quantity should get added without asking me.

Any help would be deeply appreciated.

my code here:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_detail);


btnAdd = (Button) findViewById(R.id.btnAdd);

// event listener to handle add button when clicked
btnAdd.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub
// show input dialog
inputDialog();
}
});

}


// method to show number of order form

void inputDialog(){

// open database first
try{
dbhelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle(R.string.order);
alert.setMessage(R.string.number_order);
alert.setCancelable(false);
final EditText edtQuantity = new EditText(this);
int maxLength = 3;
edtQuantity.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
edtQuantity.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(edtQuantity);

alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String temp = edtQuantity.getText().toString();
int quantity = 0;

// when add button clicked add menu to order table in database
if(!temp.equalsIgnoreCase("")){
quantity = Integer.parseInt(temp);
if(dbhelper.isDataExist(Menu_ID)){
dbhelper.updateData(Menu_ID, quantity, (Menu_price*quantity));
}else{
dbhelper.addData(Menu_ID, Menu_name, quantity, (Menu_price*quantity));
}
}else{
dialog.cancel();
}
}
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

// when cancel button clicked close dialog
dialog.cancel();
}
});

alert.show();
}

Answer

Call this method instead :-

void addtoCart(){

// open database first
try{
    dbhelper.openDataBase();
}catch(SQLException sqle){
    throw sqle;
}

 if(dbhelper.isDataExist(Menu_ID)){
                dbhelper.updateData(Menu_ID, 1, (Menu_price));
            }else{
                dbhelper.addData(Menu_ID, Menu_name, 1, (Menu_price));
            }

}

Like this in your button on click:-

   btnAdd.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        // show input dialog
        addtoCart();
       startActivity(new Intent(getApplicationContext(),CartActivity.class));

    }
});

}