0

i have recyclerview with adapter.

@Override
public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
    int positions = position;
    Bitmap bitmap = BitmapFactory.decodeByteArray(arrayList.get(position).getImg(), 0, arrayList.get(position).getImg().length);
    holder.button.setImageBitmap(bitmap);
    holder.seekBar.setProgress(arrayList.get(position).getSeek());

    holder.btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int index = arrayList.indexOf(arrayList.get(positions));
            arrayList.remove(arrayList.get(positions);
            adapter.notifyItemRemoved(index);
        }
    });
}

if i have 2 item in arrayList first, i remove item position 0. and second, if i remove item position 0 again, i have error

android IndexOutOfBoundsException: Index: 1, Size: 1

i don't know why.. deleted one item of the two items. And the position of one remaining item should be 0. but why show error?

After deleting one item, I checked that the size of the arrayList was 1.

what i have to do?

StarCue
  • 63
  • 9
  • change adapter.notifyItemRemoved(index); to notifyDataSetChanged(); – Athira Oct 26 '21 at 10:23
  • use notifyItemRangeChanged(position, getItemCount()); after notifyItemRemoved(position); https://stackoverflow.com/questions/28189371/using-notifyitemremoved-or-notifydatasetchanged-with-recyclerview-in-android – tktschool Oct 26 '21 at 11:14
  • thanks i solved the problem!! Simple way but I hadn't thought of.. thank you so much – StarCue Oct 26 '21 at 14:50

0 Answers0