How to start an activity by click of image button of a List view?

前端 未结 2 1391
一生所求
一生所求 2021-01-23 02:55

i have a list there is a image button in each item when i click on a image i have to start another Activity with intent

following

相关标签:
2条回答
  • 2021-01-23 03:28

    have you tried this:

    MainActivity.this.startActivity(zoom);
    
    0 讨论(0)
  • 2021-01-23 03:37

    You should pass your context to your custom adapter when you create it:

    public class MyAdapter extends BaseAdapter {
        private Context mContext;
    
        public MyAdapter (Context ctx) {
            mContext = ctx;
        }
    
        ...
    }
    

    And in getView use that context to start the Activity:

     holder.profilePicture.setOnClickListener(new OnClickListener() 
       { 
           public void onClick(View v) 
           {
               Log.d("OnImageButton","Clicked");
               Intent zoom=new Intent(mContext, ImageZoom.class);
               int imageID=holder.profilePicture.getId();
               zoom.putExtra("ImageId", imageID);
               mContext.startActivity(zoom)  ; //This line raises error !      
                           }
    
    
       });
    

    When you create the Adapter in your Activity you should pass "this" as a parameter:

     mAdapter = new MyAdapter(this);
    
    0 讨论(0)
提交回复
热议问题