TJ1
TJ1

Reputation: 8468

calling an activity when an item is pressed in a ListView in an Android app

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

Answers (3)

Pratik
Pratik

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

Andrew Anderson
Andrew Anderson

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

Tanmay Mandal
Tanmay Mandal

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

Related Questions