1

Explanation: I know this question asked no. of times. Try to understand my problme first.I have a listview in which i used a BaseAdapter to show dynamic item on my listivew row and it is not a fixed. I delete the data included into the row but deleted the row. While i delete a row it's data removed completely but not a listview row. I want to remove a particular row from the list.

Adapter

public class CartAdapter extends BaseAdapter {

    private static final String TAG=CartAdapter.class.getSimpleName();

    public Context context;
    public List<CartItems> cartItemList;
    public LayoutInflater inflater;
    public AppCompatActivity activity;

    public CartAdapter(Context context, List<CartItems> cartItemList,AppCompatActivity activity) {
        this.context = context;
        this.cartItemList = cartItemList;
        this.activity=activity;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return this.cartItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.cartItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    private class CartHolder {
        TextView txtItemName, txtItemPrice, txtQtyCount;
        ImageButton btnImgDelete;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        final CartHolder holder;
        if (view == null) {
            holder = new CartHolder();
            view = inflater.inflate(R.layout.cart_items, null);

            holder.txtItemName = (TextView) view.findViewById(R.id.txt_item_name);
            holder.txtItemPrice = (TextView) view.findViewById(R.id.txt_item_price);
            holder.txtQtyCount = (TextView) view.findViewById(R.id.txt_qty_count);

            holder.btnImgDelete=(ImageButton)view.findViewById(R.id.btn_img_delete);

            holder.btnImgDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {
                    final AlertDialog.Builder alertDialog=new AlertDialog.Builder(activity,R.style.MyAlertDialogStyle);
                    alertDialog.setTitle("Delete");
                    alertDialog.setIcon(R.drawable.ic_action_cancel);
                    alertDialog.setMessage("Do you really want to delete item??");
                    alertDialog.setCancelable(true);
                    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            cartItemList.remove(position);
                            notifyDataSetChanged();
                        }
                    });
                    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
                    alertDialog.show();
                }
            });
            view.setTag(holder);
        } else {
            holder = (CartHolder) view.getTag();
        }

        CartItems cartItems = cartItemList.get(position);
        holder.txtItemName.setText(cartItems.getItemName());
        holder.txtItemPrice.setText(Util.priceTODollar(cartItems.getItemPrice()));
        holder.txtQtyCount.setText("" + cartItems.getItemQty());


        return view;
    }
}

Screenshot before delete an item from the listview

enter image description here

Screenshot after delete an item from the listview

enter image description here

In the second screenshot second item deleted but the white background color remain same.

Please, help me to solve out this problem.

Android Surya
  • 544
  • 3
  • 17
Milan Gajera
  • 962
  • 2
  • 14
  • 41
  • it's already available in my post. In AlertDialog.Builder – Milan Gajera Jul 25 '16 at 05:06
  • It's about only two lines cartItemList.remove(position); notifyDataSetChanged(); – Milan Gajera Jul 25 '16 at 05:07
  • Not sure if this is your issue, but your code `cartItemList.remove(position);` is wrong, as positions change in ListView, and also Views are reused. You should save the position in the holder. – lionscribe Jul 25 '16 at 05:11
  • so many posts available you can see it in stackoverflow http://stackoverflow.com/questions/5497580/how-to-dynamically-remove-items-from-listview-on-a-button-click – Android Surya Jul 25 '16 at 05:11
  • Actually, holding position in any area will cause issues, as positions change. Rather hold the CartItem in the holder, and then when needed, call remove(item). – lionscribe Jul 25 '16 at 05:15
  • @Surya i used a baseAdapter and i want to delete an item from the baseadapter not from the activity where i pass the List<> value. I don't have an object of the listview in adapter.I must delete an item of the position of the List<>. – Milan Gajera Jul 25 '16 at 05:16
  • @MilanGajera Put a log inside getCount() to monitor your list contents after the deletion process. – Nizam Jul 25 '16 at 05:42
  • After deleting an item it decrease the size of the cartItemList. – Milan Gajera Jul 25 '16 at 05:51

2 Answers2

1

Finally, I found the solution.After a lot of effort and try. I am not sure it is feasiable solution or not.

After remove an item from the adapter i set the handler and pass the bundler to the Message object like below:

holder.btnImgDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {
                    final AlertDialog.Builder alertDialog=new AlertDialog.Builder(activity,R.style.MyAlertDialogStyle);
                    alertDialog.setTitle("Delete");
                    alertDialog.setIcon(R.drawable.ic_action_cancel);
                    alertDialog.setMessage("Do you really want to delete item??");
                    alertDialog.setCancelable(true);
                    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if(cartItemList.remove(cartItems)) {
                                notifyDataSetChanged();

                                Message message=new Message();
                                Bundle bundle=new Bundle();
                                bundle.putSerializable("KEY",(Serializable)cartItemList);
                                message.setData(bundle);
                                activity.uiHomeHandler.sendMessage(message);
                            }
                        }
                    });
                    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
                    alertDialog.show();
                }
            });

In activity i again set the cartitemList object to the baseAdapter and calling again it after remove an item

SuppressLint("HandlerLeak")
    public class UiHandler extends Handler {
        @Override
        public void handleMessage(final Message msg) {
            super.handleMessage(msg);
            Bundle b=msg.getData();
            cartItemList=(List)b.getSerializable("KEY");

            mCartAdapter=new CartAdapter(getApplicationContext(),cartItemList,CartActivity.this);
            lvCartItem.setAdapter(mCartAdapter);
            Helper.getListViewSize(lvCartItem);
        }
    }
Milan Gajera
  • 962
  • 2
  • 14
  • 41
1

Your problem is here:

cartItemList.remove(position);

As you know for the sake of performance, in list views rows will be recycled as they go off screen. So, when you're initializing a row (where view == null in the getView method), you actually don't know the position assigned to this row as this row can be assigned multiple different positions.

In order to find the position correctly you should use getPositionForView method of the list view. So, in your onClick listener replace your code with this one:

int p = ((ListView) viewGroup).getPositionForView(view);
cartItemList.remove(p);
notifyDataSetChanged();

Note that in the snippet above, viewGroup refers to the list view itself and view refers to the row being deleted. So, rename the view in onClick method so that it doesn't cause name-clashing.

Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129