Michiel T
Michiel T

Reputation: 537

Starting new intent

Im trying to start a new intent on clicking an item in a listview but dont know how to get this working.

Here is the code:

final ListView lv = getListView();
lv.setTextFilterEnabled(true);  
lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")                  
            Intent intent = new Intent(this, Profileviewer.class);  
            startActivity(intent); 
        }
});

I get an compiller error on new Intent(this, Profileviewer.class);

The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<Profileviewer>) is undefined

Upvotes: 0

Views: 230

Answers (3)

Errol Dsilva
Errol Dsilva

Reputation: 177

As you said you are trying to launch an Intent from a ListView. In your code this means the list view. That's what the error message says. You need to use your package name using any of the below methods.

  1. Intent intent = new Intent({package_name}, Profileviewer.class);
  2. Intent intent = new Intent(Profileviewer.this, Profileviewer.class);

Upvotes: 0

Mariusz Chw
Mariusz Chw

Reputation: 364

I think you forgot add a new activity in AndroidManifest.xml.

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

You should pass to the intent the context of the activity (by putting YourActivity.this), by passing only this, you are passing the AdapterView.OnItemClickListener()..

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
        @SuppressWarnings("unchecked")                  
        Intent intent = new Intent(YourActivity.this, Profileviewer.class);  
        startActivity(intent); 
    }
});

Upvotes: 4

Related Questions