How to start Activity in adapter? How to start Activity in adapter? android android

How to start Activity in adapter?


Just pass in the current Context to the Adapter constructor and store it as a field. Then inside the onClick you can use that context to call startActivity().

pseudo-code

public class MyAdapter extends Adapter {     private Context context;     public MyAdapter(Context context) {          this.context = context;          }     public View getView(...){         View v;         v.setOnClickListener(new OnClickListener() {             void onClick() {                 context.startActivity(...);             }         });     }}


When implementing the onClickListener, you can use v.getContext.startActivity.

btn.setOnClickListener(new OnClickListener() {                      @Override    public void onClick(View v) {        v.getContext().startActivity(PUT_YOUR_INTENT_HERE);    }});


public class MyAdapter extends Adapter {     private Context context;           public MyAdapter(Context context) {          this.context = context;              }     public View getView(...){           View v;           v.setOnClickListener(new OnClickListener() {             void onClick() {                  Intent intent= new Intent(context, ToActivity.class);                    intent.putExtra("your_extra","your_class_value");                 context.startActivity(intent);             }         });     }}