I have recyclerview 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 ?