'How to open a different activity on recyclerView item onclick

i am using a recyclerView to show my listitems in the navigation drawer.I have implemented the onclickListener but i have been stuck on how to open a different activity when items are clicked. All my items do on item click as of now as per the code is to display a toast with the item position.

I would appreciate the help.

AdapterClass.java

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {
    private LayoutInflater inflater;
    private Context context;
List<Information>data= Collections.emptyList();
    public AdapterClass(Context context,List<Information>data){
        this.context=context;

        inflater= LayoutInflater.from(context);
        this.data=data;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view= inflater.inflate(R.layout.custom_row,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Information current=data.get(position);
        holder.title.setText(current.title);
        holder.icon.setImageResource(current.iconId);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView title;
        ImageView icon;

        public MyViewHolder(View itemView) {
            super(itemView);
            title=(TextView)itemView.findViewById(R.id.listText);
           icon=(ImageView)itemView.findViewById(R.id.listIcon);
            itemView.setClickable(true);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            Toast.makeText(context,"The Item Clicked is: "+getPosition(),Toast.LENGTH_SHORT).show();
        }
    };
}
Log  cat error after implementing Konrad's solution

    02-27 15:24:52.833: D/AndroidRuntime(1630): --------- beginning of crash
02-27 15:24:52.834: E/AndroidRuntime(1630): FATAL EXCEPTION: main
02-27 15:24:52.834: E/AndroidRuntime(1630): Process: com.snappy.stevekamau.snappy, PID: 1630
02-27 15:24:52.834: E/AndroidRuntime(1630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.snappy.stevekamau.snappy/com.snappy.stevekamau.snappy.YourActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.os.Looper.loop(Looper.java:135)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread.main(ActivityThread.java:5221)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at java.lang.reflect.Method.invoke(Native Method)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at java.lang.reflect.Method.invoke(Method.java:372)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-27 15:24:52.834: E/AndroidRuntime(1630): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:95)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:88)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:84)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:175)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at com.snappy.stevekamau.snappy.YourActivity.onCreate(YourActivity.java:18)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.Activity.performCreate(Activity.java:5933)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-27 15:24:52.834: E/AndroidRuntime(1630):     ... 10 more
02-27 15:24:52.839: W/ActivityManager(464):   Force finishing activity com.snappy.stevekamau.snappy/.YourActivity
02-27 15:24:52.841: W/ActivityManager(464):   Force finishing activity com.snappy.stevekamau.snappy/.MainActivity


Solution 1:[1]

You can (but don't need to because the ViewHolder class is not static) create field context as is shown below:

private final Context context;

public MyViewHolder(View itemView) {
    super(itemView);
    context = itemView.getContext();
    ...
}

and on your onClick method just call sth like below:

@Override
public void onClick(View v) {          

    final Intent intent;
    switch (getAdapterPostion()){
        case 0:
           intent =  new Intent(context, FirstActivity.class);
           break;

        case 1:
            intent =  new Intent(context, SecondActivity.class);
            break;
           ...
        default:
           intent =  new Intent(context, DefaultActivity.class);
           break;
     }
    context.startActivity(intent);
}

or

@Override
public void onClick(View v) {          

    final Intent intent;
    if (getAdapterPosition() == sth){
       intent =  new Intent(context, OneActivity.class);
    } else if (getPosition() == sth2){
       intent =  new Intent(context, SecondActivity.class);
    } else {
       intent =  new Intent(context, DifferentActivity.class);
    }
    context.startActivity(intent);
}

Solution 2:[2]

Simply you can do it easy... You just need to get the context of your activity, here, from your View.

//Create intent getting the context of your View and the class where you want to go
Intent intent = new Intent(view.getContext(), YourClass.class);

//start the activity from the view/context
view.getContext().startActivity(intent); //If you are inside activity, otherwise pass context to this funtion

Remember that you need to modify AndroidManifest.xml and place the activity...

<activity
        android:name=".YourClass"
        android:label="Label for your activity"></activity>

Solution 3:[3]

you can implement your adapter's onClickListener:

  public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder>implements View.OnClickListener

and use interface with method in it

public interface mClickListener {
    public void mClick(View v, int position);
}

and in your onClick method call the method in the interface and pass it the view and position

in your main activity implement that interface

public class MainActivity extends ActionBarActivity implements AdapterClass.mClickListener

and override that method

@Override
public void onCommentsClick(View v, int position) {
    final Intent intent = new Intent(this, OtherActivity.class);
}

as its better to manage your activity transition by the activity not other classes

Solution 4:[4]

The easiest way to attach a clickListener to items of RecyclerView is within the Adapter as below:

public class MyAdapter extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {

....

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), YourActivity.class);
                startActivity(intent);
            }
        });
}

Solution 5:[5]

  • Sample, in MyRecyclerViewAdapter, add method:

    public interface ItemClickListener {
       void onItemClick(View view, int position);
    }
    
  • In MainActivity.java

    @Override
    public void onItemClick(View view, int position) {
    Context context=view.getContext();
    Intent intent=new Intent();
    switch (position){
        case 0:
            intent =  new Intent(context, ChildActivity.class);
            context.startActivity(intent);
            break;
       }
    }
    

Solution 6:[6]

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<Android> android;
Context context;
private ImageView img;
public DataAdapter(Context contextN, ArrayList<Android> android) {
    this.android = android;
    this.context=contextN;
}

@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_list, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {

    viewHolder.tv_name.setText(android.get(i).getOffer());
    viewHolder.tv_version.setText(android.get(i).getOfferType());
    Picasso.with(context).load(android.get(i).getImg()).transform(new CircleTransform()).into(img);

}

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

public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView tv_name,tv_version,tv_api_level;

    public ViewHolder(View view) {
        super(view);

        tv_name = (TextView)view.findViewById(R.id.tv_name);
        tv_version = (TextView)view.findViewById(R.id.tv_version);
        img = (ImageView) view.findViewById(R.id.img);

                context = itemView.getContext();

                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        int itemPosition = getLayoutPosition();
                        Toast.makeText(context, "" + itemPosition, Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(context,Show.class);
                        intent.putExtra("name",""+android.get(itemPosition).getOffer());
                        intent.putExtra("img",""+android.get(itemPosition).getImg());
                        context.startActivity(intent);

            }
        });
    }
}
}

Solution 7:[7]

The problem occurs in declaring context, while using Glide for ImageView or While using intent in recyclerview for item onClick. I Found this working for me which helps me to Declare context to use in Glide or Intent or Toast.

public class NoteAdapter extends FirestoreRecyclerAdapter<Note,NoteAdapter.NoteHolder> {

Context context;

public NoteAdapter(@NonNull FirestoreRecyclerOptions<Note> options) {
    super(options);
}

@Override
protected void onBindViewHolder(@NonNull NoteHolder holder, int position, @NonNull Note model) {
    holder.r_tv.setText(model.getTitle());
    Glide.with(CategoryActivity.context).load(model.getImage()).into(holder.r_iv);

    context = holder.itemView.getContext();

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, SuggestActivity.class);
            context.startActivity(i);
        }
    });

}

@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_category,parent,false);

    return new NoteHolder(v);
}

public static class NoteHolder extends RecyclerView.ViewHolder
{
    TextView r_tv;
    ImageView r_iv;

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

        r_tv = itemView.findViewById(R.id.r_tv);
        r_iv = itemView.findViewById(R.id.r_iv);


    }
}
}

Solution 8:[8]

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {
    private LayoutInflater inflater;
    private Context context;
List<Information>data= Collections.emptyList();
    public AdapterClass(Context context,List<Information>data){
        this.context=context;

        inflater= LayoutInflater.from(context);
        this.data=data;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view= inflater.inflate(R.layout.custom_row,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Information current=data.get(position);
        holder.title.setText(current.title);
        holder.icon.setImageResource(current.iconId);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView title;
        ImageView icon;

        public MyViewHolder(View itemView) {
            super(itemView);
            title=(TextView)itemView.findViewById(R.id.listText);
           icon=(ImageView)itemView.findViewById(R.id.listIcon);
            itemView.setClickable(true);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            Toast.makeText(context,"The Item Clicked is: "+getPosition(),Toast.LENGTH_SHORT).show();
        }
    };
}

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {
    private LayoutInflater inflater;
    private Context context;
List<Information>data= Collections.emptyList();
    public AdapterClass(Context context,List<Information>data){
        this.context=context;

        inflater= LayoutInflater.from(context);
        this.data=data;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view= inflater.inflate(R.layout.custom_row,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Information current=data.get(position);
        holder.title.setText(current.title);
        holder.icon.setImageResource(current.iconId);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView title;
        ImageView icon;

        public MyViewHolder(View itemView) {
            super(itemView);
            title=(TextView)itemView.findViewById(R.id.listText);
           icon=(ImageView)itemView.findViewById(R.id.listIcon);
            itemView.setClickable(true);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            Toast.makeText(context,"The Item Clicked is: "+getPosition(),Toast.LENGTH_SHORT).show();
        }
    };
}

Solution 9:[9]

This question has been asked long ago but none of the answers above helped me out, though Milad Moosavi`s answer was very close. To open a new activity from a certain position on the recycler view, the following code may help:


    @Override
    public void onBindViewHolder(@NonNull TripViewHolder holder, int position) {
        Trip currentTrip = trips.get(position);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), EditTrip.class);
                v.getContext().startActivity(intent);
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent(v.getContext(), ReadTripActivity.class);
                v.getContext().startActivity(intent);

                return false;
            }
        });
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Komal12
Solution 2 Neo
Solution 3 sresht
Solution 4 Alex
Solution 5 Sasikumar Murugesan
Solution 6 suresh madaparthi
Solution 7
Solution 8 raju dhiraj
Solution 9 Gabriel Arghire