Title: Can't start activity from class
Tags: android;class;android-activity;onitemclick
Question: I am trying to start an activity from a normal class and I can't figure out how it is done, if it can be done. On an itemClick I want to start an activity that extends the ListView class to show a list of options.
Also the class that receives the onItemClick is not an activity. I will post the code to try to visualize what i mean.
This is my onClick method in the class that wants to start a an activity.
public void onClick(View v) {
if (v.equals(this)) {
notifyObservers(this.getId());
} else if(v.equals(editButton) || v.equals(deleteButton)) {
This is where I want to start the activity to show my ListView...
}
This is my class that extends the ListView class.
public class ProfileSettings extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] mainSettings = getResources().getStringArray(R.array.mainSettings);
setListAdapter(new ArrayAdapter<String&gt;(this, R.layout.list_item, mainSettings));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?&gt; parent, View view, int position, long id) {
// Do something
}
});
Thanks in advance!
Comment: How about calling startActivity http://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)

Here is the accepted answer: I think this may help you:
"Pass the context of the activity via constructor to your class or make a static context in your activity.
With the context you can start activities like you would start them within the activity class."
class First extends Activity {
...
Second test = new Second(this);
test.start();
...
class Second {
private Context mContext;
...
public Second(Context c) { this.mContext = c; }
...
public start() { mContext.startActivity(...); }
for more detail check
http://www.anddev.org/view-layout-resource-problems-f27/starting-an-activity-from-a-non-activity-class-t14483.html

Here is another answer: Try this in your onClick
Intent i = new Intent(this, ProfileSettings.class);
startActivity(i);
EDIT:
Also dont forget to add the activity to your manifest.
Comment for this answer: and don't forget to add the new activity to the android manifest
Comment for this answer: I don't think that you can call startActivity from a non-activity class.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:09/27/11', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<android><class><android-activity><onitemclick>', 'Title': "Can't start activity from class", 'ViewCount': 2379, 'id': 7569114, 'user_AboutMe': '', 'user_AccountId': 701214.0, 'user_DisplayName': 'plast', 'user_Reputation': 213.0, 'user_Views': 17.0, 'user_id': 896312.0}}