I want to pass one or more variables between activities in android. One solution is to use global variables, and it is explained here: Android global variable which I repeat here:
You can extend the base android.app.Application class and add member variables like so:
public class MyApplication extends Application {
private String someVariable;
public String getSomeVariable() {
return someVariable;
}
public void setSomeVariable(String someVariable) {
this.someVariable = someVariable;
}
}
Then in your activities you can get and set the variable like so:
// set
((MyApplication) this.getApplication()).setSomeVariable("foo");
// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();
I want to use this and set a global variable in one of my activities when an item on a ListView is pressed. So I have this code:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
.
.
.
((MyApplication) this.getApplication()).setSomeVariable("foo");
}
});
However I get an error which says "The method getApplication() is undefined for the type new AdapterView.OnItemClickListener(){}".
I would like to know what is the reason I get this error and how to fix it, or a better way of passing a variable from one activity to the other.
Thanks,
TJ