2

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 are my code

my problem is i couldn't call startActivity() from custom adapter! !

in custom adapter's getView()

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


       });
Sam
  • 1,124
  • 1
  • 8
  • 12
Mohan Raj
  • 590
  • 3
  • 8
  • 22

2 Answers2

6

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);
PX Developer
  • 8,065
  • 7
  • 42
  • 66
0

have you tried this:

MainActivity.this.startActivity(zoom);
The Badak
  • 2,010
  • 2
  • 16
  • 28