Search code examples
javaandroidarraylistindexoutofboundsexceptioncardview

RecyclerView Crashes on Scrolldown ( IndexOutOfBoundsException: Index: 2, Size: 2 )


iam parsing data from json and pass it to an ArrayList that contains two Arraylists then pass them to a RecyclerView , The RecyclerView Shows The Data but 1 item Takes The whole page, and when i scroll down i can see the 2nd item and then it crashes

i tried what alot of ppl suggested by making the parent or recycler's view layout_heigt="wrap_content" it won't even open the page here's the recycler's view xml(activity_resultsdwldview) and the row.xml which contains the design of cardview for each row

excuse me really if this is not organized or being unclear , iam really short on time and a junior android dev with not much experience

activity_resultsdwldview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".Resultsdwldview">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyvlerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</RelativeLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="12dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/nameArray"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/URLName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp" />
        </LinearLayout>

    </android.support.v7.widget.CardView>


</LinearLayout>

Adapter Class

package com.example.imc;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {



    List<ArrayList<String>> Tests;
    ArrayList<String> urlArray;
    ArrayList<String> nameArray;

    public MainAdapter(List<ArrayList<String>> tests, ArrayList<String> urlArray, ArrayList<String> nameArray) {
        Tests = tests;
        this.urlArray = urlArray;
        this.nameArray = nameArray;
    }



    @NonNull
    @Override
    public MainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int i) {

        holder.URLName.setText( urlArray.get(i));
        holder.mFullname.setText( nameArray.get(i));


    }

    @Override
    public int getItemCount() {
            return Tests.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView URLName;
        public TextView mFullname;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            mFullname=itemView.findViewById(R.id.nameArray);
            URLName=itemView.findViewById(R.id.URLName);
        }
    }

}

Here's the Error log

E/RecyclerView: No adapter attached; skipping layout
D/EGL_emulation: eglMakeCurrent: 0xa2cede60: ver 2 0 (tinfo 0xa2cefaf0)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.imc, PID: 16616
    java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
        at java.util.ArrayList.get(ArrayList.java:411)
        at com.example.imc.MainAdapter.onBindViewHolder(MainAdapter.java:40)
        at com.example.imc.MainAdapter.onBindViewHolder(MainAdapter.java:13)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
        at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
        at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:286)
        at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:343)
        at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:359)
        at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:366)
        at android.support.v7.widget.GapWorker.run(GapWorker.java:397)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

enter code here


Solution

  • The size of your adapter is being set by:

     @Override
        public int getItemCount() {
                return Tests.size();
        }
    

    And your trying to get items from:

     @Override
        public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int i) {
    
            holder.URLName.setText( urlArray.get(i));
            holder.mFullname.setText( nameArray.get(i));
    
    
        }
    

    You need to set your adapter size to what you are trying to retrieve:

     @Override
            public int getItemCount() {
                   urlArray.size()
            }
    

    or

     @Override
        public int getItemCount() {
                return nameArray.sizes()
        }
    

    I would suggest using a single array, or possibly a map, to avoid similar issues.