Search code examples
javaandroidandroid-recyclerviewindexoutofboundsexceptiononitemclicklistener

Setting up onItemClickListener to RecyclerView items in Android


I am trying to set up an OnItemClickListener to RecyclerView items to open up a new activity when you click on an item in the RecyclerView.

I develop for Android phones, and I am working on API 19 actual device (min supported API for my app is 15). I already tried different samples online, but I am not getting around it with working code.

These are the lines which I get in the error message:

LevelActivity:

public class LevelActivity extends AppCompatActivity implements ViewHolder.OnItemListener {

    private static final String TAG = "LevelActivity";

    private List<Item> list = new ArrayList<>();

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_level);

                List<Item> list = fill_with_data();

                RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_item_list);
                RecyclerViewAdapter adapter = new RecyclerViewAdapter(list, getApplication(),this);
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(this));

            }

    public List<Item> fill_with_data() {

        List<Item> list = new ArrayList<>();

        list.add(new Item("Batman vs Superman"));
        list.add(new Item("X-Men: Apocalypse"));
        list.add(new Item("Captain America: Civil War"));
        list.add(new Item("Kung Fu Panda 3"));
        list.add(new Item("Warcraft"));
        list.add(new Item("Alice in Wonderland"));

        return list;
    }

    @Override
    public void onItemClick(int position) {

        list.get(position);
        Intent intent = new Intent(this,QuestionActivity.class);
            startActivity(intent);
    }


}

ViewHolder:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView name;
    OnItemListener mOnItemListener;

    ViewHolder(View itemView, OnItemListener onItemListener) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.row_item);
        this.mOnItemListener = onItemListener;

        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mOnItemListener.onItemClick(getAdapterPosition());
    }

    public interface OnItemListener {
        void onItemClick(int position);
    }

AndroidManifest:

        <!-- A child of a the level activity -->
        <activity android:name=".QuestionActivity"
            android:label="Question Activity"
            android:parentActivityName=".LevelActivity" >
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".LevelActivity" />>
        </activity>

If you would need more code let me know.

I expect to click on the RecyclerView list items and open up a new activity(QuestionActivity) to start with. It will be a quiz app, when you click on different levels it will open up a different start question, but same layout as QuestionActivity.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ..., PID: 1269
    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.example.marvelquiz.LevelActivity.onItemClick(LevelActivity.java:58)
        at com.example.marvelquiz.ViewHolder.onClick(ViewHolder.java:22)
        at android.view.View.performClick(View.java:4637)
        at android.view.View$PerformClick.run(View.java:19422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5586)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)

Solution

  • your list is empty.

    your error means your list is empty and you want give index 0 but your list is empty

    list.get(position);//Error line
    

    You wrongly put your data in new instance of list so your global list is still empty.

    Change this line

    List<Item> list = fill_with_data();
    

    To this

    list = fill_with_data();