igde_
igde_

Reputation: 261

android imageview onclick listener

I have a image gallery and each image is a news. so i want to click a image and open a class for that image to show the news about it. My code is:

 gallery.setOnItemClickListener(new OnItemClickListener() {

             public void onItemClick(AdapterView parent, View v, int position, long id) {
                  Intent intent1 = new Intent(this, ResimHaberGoster.class);
                intent1.putExtra("position", position);
                startActivityForResult(intent1, position);

             }

         });

at the Intent intent1 = new Intent(this, ResimHaberGoster.class); it gives me error and it changes this part to Intent intent1 = new Intent();

So how should i go to a new class by clicking on the gallery

THANKS

Upvotes: 0

Views: 4616

Answers (3)

pankajagarwal
pankajagarwal

Reputation: 13582

at Intent intent1 = new Intent(this, ResimHaberGoster.class); this indicates the OnItemClickListener object. Instead you need the activity context. say your current activity is MyActivity, then instead of this use MyActivity.thisin your intent constructor

Upvotes: 3

Houcine
Houcine

Reputation: 24181

in your code, try to change this :

Intent intent1 = new Intent(this, ResimHaberGoster.class);

with this :

Intent intent1 = new Intent(YourActivity.this, ResimHaberGoster.class);

explanation :

when you pass this to your intent, the this is a reference of the actual onItemClickListener instance and not of your activity . so, to tell the programm that the context that you want to pass to your intent is the actual instance of your activity , you should use YourActivity.this

and sorry for my english ;

hope it helps :)

Upvotes: 1

Sandy
Sandy

Reputation: 6353

frieza is absolutely right. You can also use getApplicationContext()

Upvotes: 1

Related Questions