ArrayList as global variable

I created class that extend of application like that

package com.appnetics;

import java.util.ArrayList;

import android.app.Application;

public class GlobalVariables extends Application {

    public ArrayList<Encounter> encounters;

}

and set it in manifist like this

<application
android:name="GlobalVariables"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >

in one Actiity I fill this array using this code

GlobalVariables appState = new GlobalVariables();
appState.encounters =new ArrayList<Encounter>(); 
................
then add 

when trying to use it it crash with null exception, I use it in another activity or class like that

    GlobalVariables appState = new GlobalVariables();

    Encounter EncounterObject = appState.encounters.get(position);

is there something missing please , how to fix that

Answers


It's an instance variable of the GlobalVariables class, so every time you create a new instance of GlobalVariables, you'll get a separate variable.

If you really want a global variable, it should be a static variable - but even that will only work if you're sharing data between activities in the same process. You should read the Android Application Framework FAQ for more information.


I would use the Singleton pattern here. Wikipedia link

public class GlobalVariables {
  public ArrayList<Encounter> encounters;

  private GlobalVariables() {
    encounters = new ArrayList<Encounter>();
  }

  private static GlobalVariables instance;

  public static GlobalVariables getInstance() {
    if (instance == null) instance = new GlobalVariables();
    return instance;
  }
}

And then you can access your data by using:

GlobalVariables.getInstance().encounters.get(position);

You can make singleton ([http://en.wikipedia.org/wiki/Singleton_pattern]) class that provide one instance of object. And create ArrayList veriable in this class. And the you can access this arraylist where you want. For example singleton class:

public class Singleton {

private ArrayList<Object> arrayList;

private static Singleton instance;

private Singleton(){
    arrayList = new ArrayList<Object>();
}

public static Singleton getInstance(){
    if (instance == null){
        instance = new Singleton();
    }
    return instance;
}

public ArrayList<Object> getArrayList() {
    return arrayList;
}
}

And using in activity:

public class MyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Singleton.getInstance().getArrayList();
}
}

You are Creating a new application object by calling constructor each time, instead you should get Application Reference, by method, getApplication(). So you should edit your code to:

GlobalVariables appState = (GlobalVariables) getApplication();
appState.encounters =new ArrayList<Encounter>(); 
................
then add 

and

GlobalVariables appState = (GlobalVariables) getApplication();

Encounter EncounterObject = appState.encounters.get(position);

Need Your Help

sh.exe": git: command not found

git bitbucket git-bash

I recently started using Bitbucket for some projects within my company and am having trouble with git. Whenever I use any command such as git, I receive this error:

Restkit - Cannot set the Preferred Output Date Formatter with RKDotNetDateFormatter

ios web-services restkit date-parsing

I follow the Restkit wiki and succeed to parse ASP.NET date to NSDate. But I cannot set the default output, whatever I do, it still outputs as the following format: "2010-01-27 00:00:00 +0000".