I am trying to finish activity from adapter class while clicking Recyclerview
item
using the code
public void onBindViewHolder(@NonNull CountryCodeAdapter.CountryViewHolder holder, int position{
CountryModel countryMode = countryModels.get(position);
final String cCode = countryModel.getName();
holder.llcountryCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(),RegisterActivity.class);
i.putExtra("countryCode", cCode);
v.getContext().startActivity(i);
((AppCompatActivity)context).finish();
}
}
}
Also tried this but didn't work
((Activity)context).finish();
And i get this error:
cannot be cast to android.support.v7.app.AppCompatActivity
fetch Activity instance in Adapter constructor:
public class MyAdapter extends IAdapter<RecyclerView.ViewHolder,MyDS> {
private Activity activity;
public MyAdapter(Activity activity) {
this.activity = activity;
}
}