Search code examples
androidonitemclickandroid-recyclerview

startActivity() on a RecyclerView Item


I need to start an activity based on the item a user clicks on a RecyclerView. The code below has the position as a reference. Does anyone knows how to get this done? I need something like Intent intent = new Intent (MainActivity.this, Target.class). The target class changes depending on the item clicked of course.

        mRecyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
                @Override public void onItemClick(View view, int position) {

                    Intent intent = new Intent(MainActivity.this, ???);
                    startActivity(intent);

                }
            })
    );

Solution

  • You have collection of objects (probably ArrayList),try to add Object which has field of Class type, and then get it like this:

                    Intent intent = new Intent(MainActivity.this, objects.get(position).getClassField());
                    startActivity(intent);