In the search results, there is an option for the user to add to Cart. This has been implemented by using a Button or imageButton.
The complete view on how this can be implemented is available below.
Source Code
In Search.java, we have added a onClickListener for the button and a custom onClickListener to handle the click.
inside getView of SearchResultsAdapter, we set up the custom listener
holder.addToCart.setOnClickListener(new MyPersonalClickListener("button_addtocart",tempProduct,context));
and then inside the SearchResultsAdapter class, add the following code to handle onClick
//this is a customized clicklistener public class MyPersonalClickListener implements OnClickListener { String button_name; Product prod_name; int tempQty; int tempValue; SQLiteDatabase sqLite; Context context; //constructor method public MyPersonalClickListener(String button_name, Product prod_name, Context context) { this.prod_name = prod_name; this.button_name = button_name; this.context = context; } @Override public void onClick(View v) { // in this section, we are going to add items to cart //if the item is already there in cart, then increase the quantity by 1, else add the item to cart //if the quantity of the item has reached 10, then do nothing ---this is just a specific logic where //i did not want any item with quantity more than 10, but if you choose not to, then just comment out //the code. sqLite=context.openOrCreateDatabase("basketbuddy", context.MODE_PRIVATE, null); Cursor cc = sqLite.rawQuery("SELECT PRODUCT_QTY, PRODUCT_VALUE FROM CART WHERE PRODUCT_CODE ="+Integer.parseInt(prod_name.getProductCode()), null); if (cc.getCount()== 0) { //product not already there in cart..add to cart sqLite.execSQL("INSERT INTO CART (PRODUCT_CODE, PRODUCT_NAME, PRODUCT_BARCODE, PRODUCT_GRAMMAGE"+ ", PRODUCT_MRP, PRODUCT_BBPRICE, PRODUCT_DIVISION, PRODUCT_DEPARTMENT,PRODUCT_QTY,PRODUCT_VALUE) VALUES("+ prod_name.getProductCode()+",'"+ prod_name.getProductName()+ "','" + prod_name.getProductBarcode()+"','"+ prod_name.getProductGrammage()+"',"+ Integer.parseInt(prod_name.getProductMRP())+","+ Integer.parseInt(prod_name.getProductBBPrice())+","+ Integer.parseInt(prod_name.getProductDivision())+","+Integer.parseInt(prod_name.getProductDepartment())+ ",1,"+ Integer.parseInt(prod_name.getProductBBPrice())+")"); Toast.makeText(context,"Item "+prod_name.getProductName()+" added to Cart", Toast.LENGTH_LONG).show(); } else { //product already there in cart if(cc.moveToFirst()) { do{ tempQty=cc.getInt(0); tempValue = cc.getInt(1); }while(cc.moveToNext()); } if (tempQty < 10) { sqLite.execSQL("UPDATE CART SET PRODUCT_QTY = "+ (tempQty+1)+",PRODUCT_VALUE = "+ (Integer.parseInt(prod_name.getProductBBPrice())+tempValue)+" WHERE PRODUCT_CODE ="+ prod_name.getProductCode()); Toast.makeText(context,"Item "+prod_name.getProductName()+" added to Cart", Toast.LENGTH_LONG).show(); } } sqLite.close(); } }
No comments:
Post a Comment