How to update a recyclerview item it its already exists, in shoping cart system? when i add an item 2 times then if i delete one then the second one will appear,only after deleting the first one in android studio using java
... CartModel cart = new CartModel(namee, manufacture, avail_cart, e__pnum, m__pnum, String.valueOf(p), quantityy, String.valueOf(roundoff));
if (db.addToCart(cart)) {
// Toast.makeText(getApplicationContext(), "Product added to cart " , Toast.LENGTH_LONG).show(); //roundoff
Snackbar.make(findViewById(android.R.id.content),"Product added to cart ",Snackbar.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Product not added to cart", Toast.LENGTH_LONG).show();
Snackbar.make(findViewById(android.R.id.content),"Product not added to cart ",Snackbar.LENGTH_SHORT).show();
}
...
public boolean addToCart(CartModel cartModel) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Manufacturer, cartModel.getManufacturer());
values.put(Availability, cartModel.getAvailability());
values.put(E_part, cartModel.getE_part());
values.put(M_part, cartModel.getM_part());
values.put(Unit_price, cartModel.getUnit_());
values.put(Quantity, cartModel.getQuantity());
values.put(Line_total, cartModel.getLine_total());
values.put(Name, cartModel.getName());
return db.insert(CartDetails, null, values) > 0;
}
First, you need to query the database if cartModel.getName()
already exists. If yes, use db.update else use db.insert
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.