Recyclerview and Java.Lang.Indexoutofboundsexception: Inconsistency Detected. Invalid View Holder Adapter Positionviewholder in Samsung Devices

Inconsistency Detected Invalid View Holder Adapter Error

I solved my problem. The problem happens in MainActivity, it is caused by RecyclerView Data that has been modified in different thread. checking all data access and wrapping LinearLayoutManager would be considered as a solution.

I wrote the code gave by @sakiM at first answer: RecyclerView and java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder in Samsung devices
Here is the code:

public class WrapContentLinearLayoutManager extends LinearLayoutManager {
public WrapContentLinearLayoutManager(Context context) {
super(context);
}

public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}

public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
Log.e("TAG", "meet a IOOBE in RecyclerView");
}
}
}
  • Then i matched my RecyclerView to WrapContentLinearLayoutManager

Here is the code:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

Thank you all, have a nice day

RecyclerView and Database - java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter

Ok I got the answer

Use notifyDataSetChanged() in the init block in the FriendHistoryAdapter.kt class and in the deletePlayerHistory() function call notifyItemRemoved(position) and notifyItemRangeChanged(position,friendHistoryData.size) to update the range of the final list. That's what the issue was to update the size of the list containing items in the database.

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter when using DiffUtil

Your parameters to the BookListDiffCallBacks constructor are backwards:

val result = DiffUtil.calculateDiff(BookListDiffCallBacks(ArrayList(value), ArrayList(books)))

You're passing new, old, but the constructor expects old, new.

IndexOutOfBoundsException: Inconsistency detected error while scrolling

You pass your messageList to the adapter, so the messageList in the adapter and in the activity are the same object references. Then, as I understand, somehow method fetchMessageList is called, it's where the problem appears. You call .clear() on your list and start an asynchronous operation to fetch a new list, to then synchronously post it to your adapter.

The thing is, after you have cleared your list, your adapter keeps the reference to an empty list now, without being notified about the changes. The adapter still "thinks" that your list is the same size as it was before, so when you scroll the RecyclerView, it tries to call at least onBindViewHolder for new appearing items. But, as the list is empty, it throws IndexOutOfBoundsException.

You could try to notify the adapter about the changes immediately after calling messageList.clear(), but it seems to me that just deleting this clearing will solve the problem.

private void fetchMessageList(){
//messageList.clear(); <- delete this
AndroidNetworking.get(Util.url)
...
}

android recyclerview inconsistency detected invalid recycler viewholder position

Since you're actually replacing all data, calling notifyItemRangeInserted() without actually inserting anything into the adapter raises this issue. You have to call the correct notifier just after setting the actual data to the adapter or to be more precise underlying list the list.

notifyItemRangeInserted(pos, count) notifies the adapter to expect count items inserted at pos. Therefore the size of the list should have grown by count, not just be count. Since you replace all data, you should call notifyDataSetChanged() instead or use DiffUtil to calculate the actual diff of your data. But then you don't have to worry about which notification to use anymore.

listAdapter.notifyDataSetChanged();


Related Topics



Leave a reply



Submit