IndexOutOfBoundsException on ArrayList
<p>For some reason, this method is returning <code>viewType = 2</code> at the first location in my ArrayList. Is that because 0 divided by 5 does not produce a remainder? If I set <code>&amp;&amp; position != 0</code> instead of 1 I get <code>java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0</code></p>

<pre><code>@Override
public int getItemViewType(int position) {
        int viewType = 1;
        // Ads appear where the position in the list divided by 5 does not produce a remainder, i.e. 0, 5, 10, 15, 20, 25, etc.
        if ((position % 5 == 0) &amp;&amp; position != 1) viewType = 2;
        return viewType;
}
</code></pre>

<p>Ideally I'd have my ad placement (viewType = 2) appear every 25 positions, excluding the very first one. How can I do that?</p>

<p><strong>EDIT</strong></p>

<pre><code>public class MarketFeedRecyclerAdapter extends LoadingRowRecyclerAdapter {

    private static final int VIEW_TYPE_MARKET_FEED = 1;
    private static final int VIEW_TYPE_AD = 2;

    private final Context context;
    private final List&lt;Feed&gt; feedItems;
    private final ImageLoader feedItemImageLoader;
    private FeedItemClickListener feedItemClickListener;

    private boolean isLongPressed = false;
    public MarketFeedRecyclerAdapter(Context context, List&lt;Feed&gt; feedItems, ImageLoader feedItemImageLoader, List&lt;Feed&gt; adItems) {
        this.context = context;
        this.feedItems = feedItems;
        this.feedItemImageLoader = feedItemImageLoader;
        if (!adItems.isEmpty()) { feedItems.addAll(adItems); }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_MARKET_FEED:
                return new MarketFeedViewHolder(new FeedItemView(context));
            case VIEW_TYPE_AD:
                return new AdViewHolder(new MarketFeedAdItemView(context));
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    // Differentiate between feedItem views and nativeAds
    @Override
    public int getItemViewType(int position) {
            int viewType = 1;
            // Ads appear where the position in the list divided by 5 does not produce a remainder, i.e. 0, 5, 10, 15, 20, 25, etc.
            if ((position % 5 == 0)) viewType = 2;
            return viewType;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_MARKET_FEED) {
            bindMarketFeedItemView((MarketFeedViewHolder) viewHolder, position);
        }  else {
            bindAdItemView((AdViewHolder) viewHolder);
        }
    }

    // For Ad Objects
    private void bindAdItemView(AdViewHolder viewHolder) {

        showNativeAd(viewHolder);

    }

    private void bindMarketFeedItemView(MarketFeedViewHolder viewHolder, int position) {
        final FeedItemView feedItemView = viewHolder.feedItemView;
        final Feed feedDesign = feedItems.get(position);

        // TODO we can probably conditionally show or hide these based on the type of feed item, same as in FeedActivitySingle
        feedItemView.showOrHideEditButton(false);
        feedItemView.showOrHideBuyButton(true);
        feedItemView.showOrHideFlipButton(feedDesign.getDesign().getCompressedBackImage() != null);
        feedItemView.showOrHidePriceText(true);

        // Set the results into TextViews
        feedItemView.setProductPriceText(String.valueOf(feedDesign.getDesign().getPrice()));
        feedItemView.setDownloadsText(String.valueOf(feedDesign.getDesign().getDownloadCount()));
        feedItemView.setLikesText(String.valueOf(feedDesign.getDesign().getLikesCount()));
        feedItemView.setUsernameText(feedDesign.getDesign().getAuthor().getUsername());
        feedItemView.setTimestampText(feedDesign.getTimestampText());
        feedItemView.getSaveImage().setImageResource(feedDesign.isInPersonalGallery() ? R.drawable.ic_action_saved : R.drawable.ic_not_saved);
        feedItemView.getLikeImage().setImageResource(feedDesign.isLiked() ? R.drawable.ic_action_like_feed_full : R.drawable.ic_action_like_feed);

        feedItemView.getTrashImage().setVisibility(ParseHelper.isCurrentUser(feedDesign.getDesign().getAuthor().getObjectId()) ? View.VISIBLE : View.GONE);
        feedItemView.getFeedSocialShareImage().setVisibility(View.VISIBLE);

        switch(feedDesign.getDisplayedSide()) {
            case FRONT:
                feedItemImageLoader.DisplayImage(feedDesign.getDesign().getCompressedImage().getUrl(), feedItemView.getImage(), feedItemView.getProgressBar());
                break;
            case BACK:
                feedItemImageLoader.DisplayImage(feedDesign.getDesign().getCompressedBackImage().getUrl(), feedItemView.getImage(), feedItemView.getProgressBar());
                break;
        }

        if(feedDesign.getDesign().getAuthor().getProfilePicture() != null) {
            feedItemImageLoader.DisplayImage(feedDesign.getDesign().getAuthor().getProfilePicture().getUrl(), feedItemView.getProfilePicture(), null); // TODO should this use profilePictureFileCache?
        } else {
            viewHolder.feedItemView.getProfilePicture().setImageResource(R.drawable.ic_anonymous);
        }

        SetCommentViews(feedItemView, feedDesign.getComments());
        SetClickListeners(feedItemView, feedDesign, position);
    }

    @Override
    protected int getContentDataSize() {
        return feedItems.size();
    }

    @Override
    protected int getViewType(int position) {
        return VIEW_TYPE_MARKET_FEED;
    }

    class MarketFeedViewHolder extends RecyclerView.ViewHolder {

        FeedItemView feedItemView;

        public MarketFeedViewHolder(FeedItemView view) {
            super(view);
            this.feedItemView = view;
        }
    }

    class AdViewHolder extends RecyclerView.ViewHolder {

        MarketFeedAdItemView adItemView;

        public AdViewHolder(MarketFeedAdItemView view) {
            super(view);
            this.adItemView = view;
        }

    }

    private NativeAd nativeAd;
    private AdChoicesView adChoicesView;

    private void showNativeAd(AdViewHolder viewHolder){
        AdSettings.addTestDevice("STRING");
        nativeAd = new NativeAd(context, "STRING");
        nativeAd.setAdListener(new AdListener() {

            @Override
            public void onError(Ad ad, AdError adError) {

            }

            @Override
            public void onAdLoaded(Ad ad) {

                final MarketFeedAdItemView adItemView = viewHolder.adItemView;

                // Setting the Text
                adItemView.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
                adItemView.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
                adItemView.nativeAdTitle.setText(nativeAd.getAdTitle());
                adItemView.nativeAdBody.setText(nativeAd.getAdBody());

                // Downloading and setting the ad icon
                NativeAd.Image adIcon = nativeAd.getAdIcon();
                NativeAd.downloadAndDisplayImage(adIcon, adItemView.nativeAdIcon);

                // Download and setting the cover image
                /*NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();*/
                adItemView.nativeAdMedia.setNativeAd(nativeAd);

                // Add adChoices icon
                if (adChoicesView == null) {
                    adChoicesView = new AdChoicesView(context, nativeAd, true);
                    adItemView.addView(adChoicesView, 0);
                }

                nativeAd.registerViewForInteraction(adItemView);

            }

            @Override
            public void onAdClicked(Ad ad) {

            }
        });

        nativeAd.loadAd();
    }
}
</code></pre>

<p><strong>Error Output</strong></p>

<pre><code>    java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
         at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
         at java.util.ArrayList.get(ArrayList.java:308)
         at com.elgami.market.MarketFeedRecyclerAdapter.bindMarketFeedItemView(MarketFeedRecyclerAdapter.java:94)
         at com.elgami.market.MarketFeedRecyclerAdapter.onBindViewHolder(MarketFeedRecyclerAdapter.java:79)
         at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
         at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
         at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
         at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
         at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
         at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
         at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
         at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
         at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
         at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
         at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
         at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
         at android.view.View.layout(View.java:16636)
         at android.view.ViewGroup.layout(ViewGroup.java:5437)
         at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
         at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographe
</code></pre>

<p><strong>LoadingRowRecyclerAdapter:</strong></p>

<pre><code>public abstract class LoadingRowRecyclerAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {

    public static final int ROW_VIEW_TYPE_LOADING = 72398; // obscure number
    private boolean mContainsLoadingRow;

    protected abstract int getContentDataSize();

    protected abstract int getViewType(int position);

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ROW_VIEW_TYPE_LOADING:
                return new LoadingViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_row, parent, false));
        }

        throw new IllegalArgumentException("viewType is not ROW_VIEW_TYPE_LOADING. You must handle all other values of viewType (defined by getViewType) before calling super.");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        // empty
    }

    @Override
    public final int getItemCount() {
        return mContainsLoadingRow ? (getContentDataSize() + 1) : getContentDataSize();
    }

    @Override
    public int getItemViewType(int position) {
        return (position == getContentDataSize()) ? ROW_VIEW_TYPE_LOADING : getViewType(position);
    }

    /**
     * Sets a boolean which is used by getItemCount and in turn getItemViewType to determine which view type the row should be (loading view vs. other view).
     * Should only be called when there is more results to load in an upcoming api request (determined by calling fragment).
     */
    public void toggleLoadingRowOn() {
        mContainsLoadingRow = true;
    }

    /**
     * Checks to see if a loading row exists by checking an instance boolean and removes the row / clears the boolean.
     * This helps to 'replace' a loading row with a different row.
     */
    public void toggleLoadingRowOff() {
        if (mContainsLoadingRow) {
            mContainsLoadingRow = false;

            // removes the loading row explicitly instead of allowing it to be 'pushed' down when new user suggestion rows are added.
            // this is only required to maintain consistency with the rest of the app.
            int position = getContentDataSize();
            if (position &gt;= 0) {
                notifyItemRemoved(position);
            }
        }
    }

    protected class LoadingViewHolder extends RecyclerView.ViewHolder {

        public LoadingViewHolder(View v) {
            super(v);
        }
    }
}
</code></pre>

<p><strong>LoadingRowRecyclerAdapter:</strong></p>

<pre><code>public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    private static final int VISIBLE_THRESHOLD = 5; // The minimum amount of items to have below your current scroll position before loading more
    private LinearLayoutManager mLinearLayoutManager;

    public abstract void onLoadMore();

    public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
        this.mLinearLayoutManager = linearLayoutManager;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        int totalItemCount = mLinearLayoutManager.getItemCount();
        int firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
        int visibleItemCount = recyclerView.getChildCount();

        int lastItemVisible = firstVisibleItem + visibleItemCount;

        // once the last visible item is within VISIBLE_THRESHOLD from the bottom, we want to load more
        if ((totalItemCount - lastItemVisible) &lt;= VISIBLE_THRESHOLD) {
            onLoadMore();
        }
    }
}
</code></pre>

<p><strong>NEW STACKTRACE</strong></p>

<pre><code>java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
   at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
   at java.util.ArrayList.get(ArrayList.java:308)
   at com.elgami.market.MarketFeedRecyclerAdapter.bindMarketFeedItemView(MarketFeedRecyclerAdapter.java:93)
   at com.elgami.market.MarketFeedRecyclerAdapter.onBindViewHolder(MarketFeedRecyclerAdapter.java:78)
   at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
   at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
   at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
   at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
   at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
   at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
   at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
   at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
   at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
   at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
   at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
   at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
   at android.view.View.layout(View.java:16636)
   at android.view.ViewGroup.layout(ViewGroup.java:5437)
   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
   at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreograph
</code></pre>