Answer the question
In order to leave comments, you need to log in
How to fix app crash when scrolling recycler view?
I created a recycler view with a header, but when I scroll the application it crashes and an error appears
java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.get(ArrayList.java:437)
at com.example.testapp147.adapter.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:51)
CardViewObject profiles = postArrayList.get(position);
onBindViewHolder
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HeaderViewHolder) {
HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.headerCity.setText("");
headerHolder.headerCountry.setText("");
headerHolder.headerTime.setText("");
headerHolder.headerTemperature.setText("");
} else if (holder instanceof ItemViewHolder) {
CardViewObject profiles = postArrayList.get(position);
((ItemViewHolder) holder).avatar.setImageResource(profiles.getCardAvatar());
((ItemViewHolder) holder).name.setText(profiles.getCardName());
((ItemViewHolder) holder).location.setText(profiles.getCardLocation());
((ItemViewHolder) holder).cardImage.setImageResource(profiles.getCardImage());
((ItemViewHolder) holder).description.setText(profiles.getCardDescription());
}
}
Answer the question
In order to leave comments, you need to log in
CardViewObject profiles = postArrayList.get(position - 1);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question