开发者

Instantiate singleton class objects in Android

Can anyone tell me where to instantiate a singleton class object like ArrayList?

I am trying to instantiate in the onCreate() method but its not instantiated. Can any one tell me where to all开发者_StackOverflowocate it?

public class SharedVariables extends Application 
{
public static ArrayList<ArrayList<Map<String,String>>>  ArrayOfImageUrlDicts;
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageNameDicts;

@Override
public void onCreate()
{
    super.onCreate();
    ArrayOfImageUrlDicts  = new ArrayList<ArrayList<Map<String,String>>>(); 
    ArrayOfImageNameDicts = new ArrayList<ArrayList<Map<String,String>>>();
}
}


This is actually not a real Singelton (see Wikipedia for an accurate Description of Singeltons) but a normal static variable. If you want to usemore static variables you may consider using this as it makes things much more beautiful. However to answer your concrete question you can either use a static block where you initialize those variables or initialize them directly after decleration.

public class SharedVariables extends Application 
{
   public static ArrayList<ArrayList<Map<String,String>>>  ArrayOfImageUrlDicts = new ArrayList<ArrayList<Map<String,String>>>();
   public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageNameDicts = new ArrayList<ArrayList<Map<String,String>>>();

   @Override
   public void onCreate()
   {
      super.onCreate();
   }
}

or

public class SharedVariables extends Application 
{
   public static ArrayList<ArrayList<Map<String,String>>>  ArrayOfImageUrlDicts;
   public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageNameDicts;
   static 
   {
      ArrayOfImageUrlDicts = new ArrayList<ArrayList<Map<String,String>>>();
      ArrayOfImageNameDicts = = new ArrayList<ArrayList<Map<String,String>>>();
   }
   @Override
   public void onCreate()
   {
      super.onCreate();
   }
}


public class SharedVariables extends Application 
{

ModelSingleTonclass obj;

public static ArrayList<ArrayList<Map<String,String>>>  ArrayOfImageUrlDicts;
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageNameDicts;

@Override
public void onCreate()
{
    super.onCreate();

obj=ModelSingleTonclass.getInstance();

    ArrayOfImageUrlDicts  = new ArrayList<ArrayList<Map<String,String>>>(); 
    ArrayOfImageNameDicts = new ArrayList<ArrayList<Map<String,String>>>();
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜