通过将相同的物品添加到购物车的RecyclerView中,会出现覆盖现象。

huangapple go评论97阅读模式
英文:

By adding same item to recyclerview in shopping cart its overriding

问题

如何在购物车系统中更新已存在的 RecyclerView 项?当我添加一个项目两次,然后如果我删除一个项目,那么第二个项目将会出现,仅在删除第一个项目后才会出现,使用 Java 在 Android Studio 中实现。

...
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;
}

请注意,这段代码是关于在购物车系统中添加商品并处理重复项的逻辑,以及向数据库中添加数据的方法。如果您有其他问题或需要进一步的帮助,请随时提问。

英文:

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;


}

答案1

得分: 0

首先,您需要查询数据库,看是否已经存在 cartModel.getName()。如果是,则使用 db.update,否则使用 db.insert

英文:

First, you need to query the database if cartModel.getName() already exists. If yes, use db.update else use db.insert.

huangapple
  • 本文由 发表于 2020年4月5日 23:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61044750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定