Friday 15 February 2013

android - Create global variables and get error when set variable inside an OnClickListener ListView -


I have seen through many posts and have tried to do them, but nothing is working It seems that this has probably missed something. It is what I am trying to do and my problem: I am trying to create a global variable, so that on my main activity, when a user lists a item If I select, I assign that item to the variable Do not want it to use through my application

Faiars created a class and it is transmitted in the form of application:

Import android.app.Application;

Public class GlobalVariables applications expand {

  Private string mStringValue; Public String Some Variables () {return mStringValue; } Public Zero Set Some Variables (String Some Variables) {this.mStringValue = someVariable; }   

}

} Then in my main activity, I have put this code to:

  public class Expansion of MainActivity Activity {@ Override Public Wide On Crate (Bundle Saved Instance) {Super. Contents (Saved Instance State); SetContentView (R.layout.activity_main); ListViewksetOnItemClickListener (new OnItemClickListener () {@Override public IteemClick zero (adapter sequence & lt ;? & Gt; ListView, views, int position, long id) the cursor is located in the same row set cursor {// Results Do = (cursor) ListViewkgetItemAtPosition (status); // client name received string of the row in the database countryCode = cursor.getString (cursor.getColumnIndexOrThrow ( "CUSTOMERNAME")) ;. Toast.makeText (getApplicationContext (), country Code, Toast.LENGTH_SHORT) Show (); (GlobalViribles) this.getApplication (). SetSomeV Ariable (countrycode);}});   

The problem is that I am getting error "method getApplication () thus defined for the new adapter sequence. OnItemClickListener () {}" on this line: < Pre> ((GlobalVariables) this.getApplication ()). SetSomeVariable (countryCode);

I have suggested following a post, but later I get another error:

  • The method is mandatory for getApplication () type GlobalVariables
  • type any enclosing example GlobalVariables can be accessed under the

    ((GlobalVariables) GlobalVariables.this.getApplication ()). SetSomeVariable ("foo");

    Any help you can give will be highly appreciated.

    Thanks,

    Tim

    OnItemClickListener () is an anonymous-internal class, it means that when you this inside it, this actually type the object OnItemClickListener , Which refers to any method, try getApplication ()

    .

      ((GlobalVariables) MainActivity.this.getApplication ()) setSomeVariable (country code);    

No comments:

Post a Comment