How to start Activity in adapter?

前端 未结 8 1202
执念已碎
执念已碎 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:27

    callback from adapter to activity can be done using registering listener in form of interface: Make an interface:

          public MyInterface{
             public void  yourmethod(//incase needs parameters );
             }
    

    In Adapter Let's Say MyAdapter:

        public MyAdapter extends BaseAdapter{
           private MyInterface listener;
    
        MyAdapter(Context context){
            try {
                this. listener = (( MyInterface ) context);
                  } catch (ClassCastException e) {
                   throw new ClassCastException("Activity must implement MyInterface");
              }
    

    //do this where u need to fire listener l

              try {
                    listener . yourmethod ();
                } catch (ClassCastException exception) {
                   // do something
                }
    
          In Activity Implement your method:
    
    
             MyActivity extends AppCompatActivity implements MyInterface{
    
                    yourmethod(){
                    //do whatever you want
                         }
                         }
    
    0 讨论(0)
  • 2020-11-27 03:29

    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);
        }
    });
    
    0 讨论(0)
  • 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(...);
                 }
             });
         }
    }
    
    0 讨论(0)
  • 2020-11-27 03:42

    For newer versions of sdk you have to set flag activity task.

    public void onClick(View v)
     {
         Intent myactivity = new Intent(context.getApplicationContext(), OtherActivity.class);
         myactivity.addFlags(FLAG_ACTIVITY_NEW_TASK);
         context.getApplicationContext().startActivity(myactivity);
     }
    
    0 讨论(0)
  • 2020-11-27 03:45
    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);
                 }
             });
         }
    }
    
    0 讨论(0)
  • 2020-11-27 03:45

    If you want to redirect on url instead of activity from your adapter class then pass context of with startactivity.

    btnInstall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(link));
                    intent.setData(Uri.parse(link));
                    context.startActivity(intent);
                }
            });
    
    0 讨论(0)
提交回复
热议问题