Q:

java.lang.IndexOutOfBoundsException: Invalid index

I have an arraylist and am using an ImageButton to delete items from the list. If I delete 3 items from the same position the app crashes with an IndexOutOfBoundsException. I can't figure how to change the array position of an item after deleting the items that come before it.
 @Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    ExRow expense = expenseList.get(position);
    holder.title.setText(expense.getTitle());
    holder.amount.setText(expense.getAmount());
    mRemoveButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            // Remove the item on remove/button click
            expenseList.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, expenseList.size());

        }
    });
}

A:

There are many possible solutions for this.
This one is ideal according to me
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    ExRow expense = expenseList.get(holder.getAdapterPosition());
    holder.title.setText(expense.getTitle());
    holder.amount.setText(expense.getAmount());
    mRemoveButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // Remove the item on remove/button click
            int adapterPosition = holder.getAdapterPosition();
            expenseList.remove(adapterPosition);
            notifyItemRemoved(adapterPosition);
            notifyDatasetChanged();
        }
    });
}

