java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder
See original GitHub issueHi Davideas & developers,
I’m facing to a strange problem and I don’t know if I do something wrong with the FlexibleAdapter.
To explain the situation :
In a form, I can open different list of choices from some clicks on edit text view. To fill the list of choices, I’m using a content provider that gets back a cursor from database fulfilled with some HEADER type and SUBITEM type. Both are stored in a LIST of storage choice model.
This LIST is then used as input of my implementation of FlexibleAdapter.
At this step, I’m reading all the list content and create ExpandableHeaderItem if type is Header (with sticky feature activated) and SubItem is other… The process of creation is quite simple, see below
items.clear(); // flush old items
ExpandableHeaderItem currentHeader = null;
for (int i = 0; i < choiceModelList.size(); i++) {
// HEADER
if (choiceModelList.get(i).getItemType() == ChoiceModel.GROUP_TYPE) {
currentHeader = new ExpandableHeaderItem(choiceModelList.get(i));
items.add(currentHeader);
} else if (choiceModelList.get(i).getItemType() == ChoiceModel.ITEM_TYPE) {
// ITEM
SubItem subItem = new SubItem(choiceModelList.get(i), currentHeader);
if (currentHeader != null) {
currentHeader.addSubItem(subItem);
} else {
items.add(subItem);
}
}
}
So I’m using ExpandableHeaderItem extending
AbstractModelItem<ExpandableHeaderItem.ExpandableHeaderViewHolder>
and SubItem extends AbstractModelItem<SubItem.ChildViewHolder>
And Sticky is initialized by
@Override
public ViewGroup getStickySectionHeadersHolder() {
FrameLayout frameLayout = new FrameLayout(mRecyclerView.getContext());
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,//or MATCH_PARENT
ViewGroup.LayoutParams.WRAP_CONTENT));
//This is important otherwise the Header disappears!
((ViewGroup) mRecyclerView.getParent()).addView(frameLayout);
return (ViewGroup) LayoutInflater.from(AproPlanApplication.getAppContext()).inflate(R.layout.sticky_header_layout, frameLayout);
}
On content initialization all seems is perfect, I get my complete list of headers and subitems for each headers. And I can click on header and its content collapsed/uncollapsed -> Working pretty well !!! The same for sticky behavior, headers are pushing others even if expanded or collapsed.
So all works fine, I choose element and quit the list to open another with another content build exactly as the previous…from cursor, etc …
Then troubles begin, this time I click on the first or second sticky headers…and boom my app is crashing… But if there’s a third header…it collapsed normally.
The crash is due to :
UncaughtException: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{d542b3a position=2 id=-1612110282, oldPos=2, pLpos:-1 scrap [attachedScrap] tmpDetached no parent} at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition (RecyclerView.java:5249) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline (RecyclerView.java:5431) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5392) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5388)… etc
So it’s seems I try to get a header item from my recyclerView that is not available anymore out of boundaries…
I also see this print on my log appearing sometimes when I scroll the list content with collapsed header content.
View: isRecyclable decremented below 0: unmatched pair of setIsRecyable() calls for ViewHolder{ade43eb position=3 id=-32702395, oldPos=-1, pLpos:-1 no parent}
I tried with or without Sticky activated. I tried with recyclerView.setHasFixedSize(true | false);
Do you already have this kind of problem ? Any hint will be appreciated …
Can you tell me if I need to use some initialization or refresh tips ? Manually add setIsRecyclable(true) or setIsRecyclable(false) somewhere…
Thanks in advance. Lo.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
I found my mistake. I was not a question of copy of dataset neither notify* methods… In my dataset’s content sometimes I used same values as title in views and in my abstract model I was using a wrong implementation of “id”. I used to get the id and hash based on the title…but like I said sometimes this value was the same than another item in the content of the recycler… I was hashing this value based on the same value -> same hash… So recycler was fighting with problematic of id.
So to resolve that issue… I just added a unique id mechanism.
public AbstractModelItem() { this.id = UUID.randomUUID().toString(); }
so I’m ensure that my id is really unique !!! Now it’s perfectly working in folding/unfolding header content.
Good, but still don’t call the methods notify**, let the adapter do it for you, check the code of the adapter so you understand.