Reputation: 537
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
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.
Intent intent = new Intent({package_name}, Profileviewer.class);
Intent intent = new Intent(Profileviewer.this, Profileviewer.class);
Upvotes: 0
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