0

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
savepopulation
  • 11,736
  • 4
  • 55
  • 80
Rocky
  • 457
  • 4
  • 11

4 Answers4

8

Make an interface for the adapter (inside the adapter class)like this :

public interface YourAdapterInteraction {
        void onClickCountryCode();
 }

Make your Activity implement your interface, like this:

public class YourActivity extends AppCompatActivity implements YourAdapter.YourAdapterInteraction

Inside YourActivity :

@Override
    public void onClickCountryCode() {
        Intent i = new 
        Intent(this,RegisterActivity.class);
        i.putExtra("countryCode", cCode);
        startActivity(i);
        finish();
 }
Hadas
  • 906
  • 12
  • 22
1

Do this :

public void onBindViewHolder(@NonNull                                                                                     
    CountryCodeAdapter.CountryViewHolder holder, int position{
      CountryModel countryModel = countryModels.get(position); 

      final String name = countryModel.getName();

      holder.llcountryCode.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent i = new Intent(context,RegisterActivity.class);
        i.putExtra("countryCode", cCode);
        context.startActivity(i);
        ((Activity)context).finish();
          }
        }
    });

instaed of :

Public void onBindViewHolder(@NonNull                                                                                     
    CountryCodeAdapter.CountryViewHolder holder, int position{
      CountryModel countryModel = countryModels.get(position); 

      final String name = 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();
          }
        }
    });
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
  • You cannot guarante that the view's Context is always `Activity`. This can crash on API 19, because the view's direct context is a `ContextThemeWrapper`. See https://stackoverflow.com/a/27434760/2413303 – EpicPandaForce Oct 17 '18 at 13:16
  • this is only for activity if you are using fragment then it will happen – Abhinav Gupta Oct 17 '18 at 13:20
  • That answer is from 2011 and was added way before android.support.v4 started adding backward compatibility theme support. – EpicPandaForce Oct 17 '18 at 13:21
1

fetch Activity instance in Adapter constructor:

 public class MyAdapter extends IAdapter<RecyclerView.ViewHolder,MyDS> {
    private Activity activity;

    public MyAdapter(Activity activity) {
        this.activity = activity;
    }

  }
Kishita Variya
  • 810
  • 9
  • 19
0

Create an interface callback inside your recyclerView, implement the method in your activity and call the method when an item is clicked.

// in your recyclerView
public interface RecyclerViewCallback {
        void onItemClick(Item item)
}
// in your activity
@Override
void onItemClicked(Item item) {
        this.finish();
}
Nicolas M.
  • 360
  • 6
  • 12