I need a way around to tackle the following IndexOutOfBoundsException [closed]

The exception originates from my_data.get(position) in your onProgressChanged() listener.

This listener is called asynchronously, when progress changes, but it refers to the original position provided, when you perform the onBindViewHolder().

So when at time X you do the onBindViewHolder(), position with value 2 is valid (if there are at least 3 entries in the list). The listener will keep this value 2 and hold on to it.

Now, if you delete items and only have 2 items left, position = 2 is no longer valid, but the listener still keeps that value and when it is called, it tries to access my_data at position = 2, which has now become invalid.

To fix this, you will have to make the listener hold on to the actual data, not the position. You can do this like so:

public void onBindViewHolder(final ViewHolder holder, final int position)
{
    final SomeClass data = my_data.get(position);

    holder.seekBar.setOnSeekBarChangeListener(new CircularSeekBar.OnCircularSeekBarChangeListener() {
        @Override
        public void onProgressChanged(CircularSeekBar circularSeekBar, int progress, boolean fromUser)
        {
            holder.seekBar.setProgress(holder.seekBar.getProgress());
            holder.actual_estimate.setText(holder.seekBar.getProgress()+"https://stackoverflow.com/" + data.getEstimate());
        }

Leave a Comment