How to open a different activity on recyclerView item onclick
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);
}
How to open different activities with Recycle view items using on click listeners
viewHolder.tv_country.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(countries.get(i).equalsIgnoreCase("India")){
// open activiy here
}
else if(countries.get(i).equalsIgnoreCase("Germany")){
// open activiy here
}
.
.
.
});
How to open few different activites on recyclerview different item click using if or case method?
here is your onClick()
code
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position == 0) {
Intent intent = new Intent(v.getContext(), FirstActivity.class);
mContext.startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(v.getContext(), SecondActivity.class);
mContext.startActivity(intent);
} else if (position == 2) {
Intent intent = new Intent(v.getContext(), ThirdActivity.class);
mContext.startActivity(intent);
} else if (position == 3) {
Intent intent = new Intent(v.getContext(), FourthActivity.class);
mContext.startActivity(intent);
}
}
});
Related Topics
How to Add a Line Break in an Android Textview
Android: Viewpager and Horizontalscrollview
How Do Detect Android Tablets in General. Useragent
Android Get Orientation of a Camera Bitmap? and Rotate Back -90 Degrees
Installation Failed Due To: 'Null' - Android Studio 3.5
How to Convert Views to Bitmaps
How to Group Radiobutton from Different Linearlayouts
Error: "Adb Connection Error:An Existing Connection Was Forcibly Closed by the Remote Host"
How to Restrict Android App to Specific Device Make
How to Convert Image into Byte Array and Byte Array to Base64 String in Android
How to Make an Imageview in Circular Shape
Get the Uri of an Image Stored in Drawable
How to Use Both Ontouch and Onclick for an Imagebutton
Creating a Custom Layout for Preferences
How to Open Email Program via Intents (But Only an Email Program)
Android Studio - Android Emulator Wifi Connected with No Internet