0

I have in my application. Using Interface in Adapter class I am deleting the items from Recyclerview.But when last item is deleted, it is still visible in RecyclerView.After manually again calling the same Fragment it get removed.This is code from Adapter class to call delete method

 public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private  TextView number;
        private  TextView people;
        private TextView myDate;
        private TextView myTime;
        private TextView myNumber;
        private TextView reservationId;
        private TextView foodieId;
        private ImageView deleteReservation;
        private ImageView editReservation;
        public ViewHolder(final View itemView, final OnEntryClickListener listener) {
            super(itemView);
            name=(TextView)itemView.findViewById(R.id.name);
            number=(TextView) itemView.findViewById(R.id.number);
            deleteEntry=(ImageView)itemView.findViewById(R.id.delete);
            editReservation=(ImageView)itemView.findViewById(R.id.editReservation);
            deleteEntry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null) {
                        int position = originalList.indexOf(filter.get(getAdapterPosition()));
                        if (position != RecyclerView.NO_POSITION) {
                            listener.deleteEntry(position);
                        }
                    }
                }
            });        
        }
    }  

This is code for deleting item in Recyclerview class where it is displayed.

@Override
                                public void deleteEntry(final int position) {
                                    AlertDialog.Builder confirm = new AlertDialog.Builder(getActivity());
                                    confirm.setTitle("Confirmation");
                                    confirm.setMessage("Are you sure to cancel reservation ?");
                                    confirm.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                                                                            removeItem(position);
                                        }
                                    });
                                    confirm.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            alertDialog.cancel();
                                        }
                                    });
                                    confirm.show();
                                }  

This is method to delete item

public void removeItem(int position) {

                current.remove(position);
                adapter.notifyItemRemoved(position);
                adapter.notifyItemRangeChanged(position, current.size()-1);

            }  

What should be the change to remove the last item which is visible after deleting also ?

Satyam Gondhale
  • 1,415
  • 1
  • 16
  • 43

4 Answers4

1

Just replace current.size()-1 by current.size() like this:

//case size == 1
current.remove(position);

//size == 0 here
adapter.notifyItemRemoved(position);

//just call current.size()
adapter.notifyItemRangeChanged(position, current.size());
maheryhaja
  • 1,617
  • 11
  • 18
0

Also item remove from arraylist like this

 public void deleteEntry(final int position) {
    AlertDialog.Builder confirm = new AlertDialog.Builder(getActivity());
    confirm.setTitle("Confirmation");
    confirm.setMessage("Are you sure to cancel reservation ?");
    confirm.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            current.remove(position);
            yourAdapter.notifyDataSetChanged();

        }
    });
    confirm.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.cancel();
        }
    });
    confirm.show();
}
mehul chauhan
  • 1,792
  • 11
  • 26
0
                               public void deleteEntry(final int position) {
                                AlertDialog.Builder confirm = new AlertDialog.Builder(getActivity());
                                confirm.setTitle("Confirmation");
                                confirm.setMessage("Are you sure to cancel reservation ?");
                                confirm.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                      current.remove(position);
                                      notifyDataSetChanged();

                                    }
                                });
                                confirm.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        alertDialog.cancel();
                                    }
                                });
                                confirm.show();
                            }  
swati
  • 1,263
  • 2
  • 17
  • 27
0

Use this;

        int position = myMatches.indexOf(r);
        if (position > -1) {
            your_list.remove(position);

            notifyItemRemoved(position);
        }
Don Louis
  • 61
  • 1
  • 7