How to start Activity in adapter?

前端 未结 8 1241
执念已碎
执念已碎 2020-11-27 03:16

I have a ListActivity with my customized adapter and inside each of the view, it may have some buttons, in which I need to implement OnClickListener. I need to

8条回答
  •  情深已故
    2020-11-27 03:39

    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(...);
                 }
             });
         }
    }
    

提交回复
热议问题