1

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

Community
  • 1
  • 1
TJ1
  • 7,578
  • 19
  • 76
  • 119

4 Answers4

4

Inside an Anonymous Class you cant say this. u have to specifi the Class around the inner class.

like MyApplication.this.get..

when using:

class MyApplication { 
  lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          ((MyApplication) MyApplication.this.getApplication()).setSomeVariable("foo");
        }
 }

}

the "this." inside an annoymous inner class refering to the inner class itself.

Michele
  • 6,126
  • 2
  • 41
  • 45
  • MyApplication is a different class than the place I want to use setSomeVariable(). Lets say I wan to do it inside MyOtherApplication, while setSomeVariable method is defined in MyApplication. So what you suggested is not working as it is. – TJ1 Jul 16 '11 at 16:47
  • Thats totally fine to with the code i wrote, the method "getApplication()" getting you your desired class, when you inside the class MyOtherApplication you have to write MyOtherApplication.this... because you calling it inside an annonymous class and the keyword "this" refering to the inner class. – Michele Jul 16 '11 at 20:15
1

Any particular reason you are not using Intent's? http://developer.android.com/guide/topics/fundamentals/activities.html#StartingAnActivity

source.rar
  • 8,002
  • 10
  • 50
  • 82
0

You have the use MyClass.this.getApplication() if you've define the anonymous OnItemClickListener in a member function of MyClass. OnItemClickListener does not know about the application.

P.Melch
  • 8,066
  • 43
  • 40
  • when I used MyClass.this.getApplication() I get another error that says: "cannot cast from Application to MyApplication". – TJ1 Jul 16 '11 at 15:42
0

I don't think you need to cast it... Application is already there.

MyApplication.getSomeVariable();

should work just like any other class.

rf43
  • 4,385
  • 3
  • 23
  • 28