0

I have develop an app, in which i have an created a global Activity class which stores all my list and boolean varaiable which can be use in any Activity class and also in non activity class. but when i want to use the getApplication() in my non activity class it gives me error .

the sample code This is my Application which has all the list and boolean variables to be used

public class MyApplicationData extends Application
{
    public  String USER_ID = "";
        public  double user_latitude;
        public  double user_logitude;

        // for password
        public  boolean remember_password = false;
        public ArrayList<Integer>value= new ArrayList<Integer>();
}

I have Another class which is non Activity where i want to use my list to save some data For your reference here is sample code

public class Test
{    
    public MyApplicationData myApplicationData;
    GetApplication getApplicationData ;
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException 
    {
        super.characters(ch, start, length);
        String data = new String(ch,start,length);
        if(in_userid)
        {
            XMLData.user_info_map.put("userid",data);
            Log.v("--: PARSED DATA :-- MAP"+i, "userid = "+data);
        }
        if(in_firstname)
            {
            XMLData.user_info_map.put("firstname",data);
            Log.v("--: PARSED DATA :-- MAP"+i, "firstname = "+data);

        }
     } 
 } 

I have class which extends Activity

public class GetApplication extends Activity
{
  public MyApplicationData getTheApplication()
  {

      return (MyApplicationData)getApplication();
  }
}

I am stuck here can any one help how to store values using Application you help will be highly obliged

jmishra
  • 2,086
  • 2
  • 24
  • 38
shvivek
  • 328
  • 3
  • 6
  • 18

2 Answers2

0

You could try to store the value in a preferences & call it anywhere as per your requirement.

Karthik
  • 4,943
  • 19
  • 53
  • 86
0

I'm not sure what you mean by a "global" Activity class (an Activity class typically represents a UI view), but if you're looking to persist data for access by multiple activities, something like a SharedPreferences might work for you:

http://developer.android.com/guide/topics/data/data-storage.html#pref

If you mean how to access the application context, and store stuff in the application. This SO answer seems to accomplish what you want:

How to declare global variables in Android?

Community
  • 1
  • 1
dule
  • 17,798
  • 4
  • 39
  • 38