Michiel T Michiel T - 28 days ago 10
Android Question

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

Answer

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); 
    }
});
Comments