Skip to content Skip to sidebar Skip to footer

List Is Returning One After Clearing All The Items By Deleting

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 hap

Solution 1:

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
        publicvoidonClick(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);
        }
    });

Solution 2:

my problem is solved by putting cartModelList.clear on delete button when cartModelList.size() == 1 , so after deleting the last item it will clear the list.

Post a Comment for "List Is Returning One After Clearing All The Items By Deleting"