List is returning one after clearing all the items by deleting

后端 未结 2 1337
猫巷女王i
猫巷女王i 2021-01-25 22:38

My List is returning an item after clearing all the items by deleting ,On app fresh install its returing null which is good but after adding item and then by deleting all, this

2条回答
  •  迷失自我
    2021-01-25 23:13

    Okay... The first.

    if (db.deleteProduct(cartModelList.get(position).getID())) 
    

    will not delete your item from cartModelList, you need to do it manually. Like this:

    if (db.deleteProduct(cartModelList.get(position).getID())) {
        cartModelList.remove(position)
    

    And the second. You have to call notifyDataSetChanged() or itemChanged or itemRemoved etc. only in the end of your deletion method. Please, tell me, if it worked.

    P.S. Your items do not cached. The problem is in your code order.

    Edit 1. Also, you need to check your db.deleteProduct method. Is it worked? Is your if statement worked?

    Edit 2. Try this.

    holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (db.deleteProduct(cartModelList.get(position).getID())) {
                        cartModelList.remove(position);
                        notifyItemRemoved(position);
    
                    Toast.makeText(context, "Product  deleted from cart", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(context, "Product not deleted from cart", Toast.LENGTH_LONG).show();
                }
    
                CartList user111 = new CartList(cartModelList.size());
    
                //  Toast.makeText(context, "else", Toast.LENGTH_SHORT).show();
                SharedPrefManager.getInstance(context).cartList(user111);
                ((Activity)context).invalidateOptionsMenu();
                ((Activity)context).finish();
                Intent intent = new Intent(context, CartActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                context.startActivity(intent);
            }
        });
    

提交回复
热议问题