tags:

views:

86

answers:

2

I have implemented a ListView in my Android application. I bind to this ListView using a custom subclass of the ArrayAdapter class. Inside the overridden ArrayAdapter.getView(...) method, I assign an OnClickListener. In the onClick(View v) method of the OnClickListener, I want to launch a new activity. I get the exception:

Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

How can I get the context that the ListView (the current Activity) is working under?

Thanks for any help.

+2  A: 

I think maybe you are implementing the OnClickListener in the wrong place - usually you should definitely implement an OnItemClickListener in your Activity and set it on the ListView instead, or you will get problems with your events...

mreichelt
You lead me to the solution. I needed to use an OnItemClickListener, assigned to the ListView. Here are some links for anyone else:http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.htmlhttp://www.androidpeople.com/android-custom-listview-tutorial-example-part-2/Thanks for the help.
Sako73
A: 

Either cache context object via constructor in your adapter or get it from your view or as last resort add - FLAG_ACTIVITY_NEW_TASK flag to your intent

myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Edit - i would avoid setting flags as it will interfere with normal flow of event and history stack.

Alex Volovoy