Reputation: 8468
I would like to call one of the activities that I have written in my android app, when I press an item in a ListView. The code that I want to use is this:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
.
.
.
Intent lp = new Intent(this, myFirst.class);
startActivity(lp);
}
});
However it I get error related to the following two lines:
Intent lp = new Intent(this, myFirst.class);
startActivity(lp);
basically I want to call "myFirst" activity when an item on my list view is pressed. What is the correct way of calling "myFirst" activity here?
Thanks for the help.
TJ
Upvotes: 0
Views: 1246
Reputation: 30855
you can do like this way
Intent lp = new Intent(<filename>.this,myFirst.class);
or
Intent lp = new Intent(getApplicationContext(),myFirst.class);
lp.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(lp);
Upvotes: 0
Reputation: 457
I think I can help you in this. 'this' may not be refering to your current activity.
Instead of 'this', use the following.
NameOfTheActivityThatIsCurrentlyOpen.this(i.e., the filename.this). This always works.
Upvotes: 1
Reputation: 40168
You are getting this error because you are passing an instance of OnItemClickListener
to your intent.
Try to pass the instance of Actiity
Intent lp = new Intent(YourActivity.this, myFirst.class);
startActivity(lp);
Upvotes: 4