D
D
dimasik1002002018-03-10 10:20:54
Java
dimasik100200, 2018-03-10 10:20:54

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)

logcat points to line 51
CardViewObject profiles = postArrayList.get(position);

She is inonBindViewHolder
@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());
        }
    }

How to fix this error? I'll throw off the entire adapter code in the comments.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Yudakov, 2018-03-10
@dimasik100200

CardViewObject profiles = postArrayList.get(position - 1);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question