在Android中实例化单例类对象

发布于 2024-10-29 12:07:12 字数 643 浏览 0 评论 0原文

谁能告诉我在哪里实例化像 ArrayList 这样的单例类对象?

我正在尝试在 onCreate() 方法中实例化 但它没有实例化。谁能告诉我分配到哪里?

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>>>();
}
}

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 allocate 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>>>();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云淡风轻 2024-11-05 12:07:12

这实际上不是真正的辛格尔顿(有关辛格尔顿的准确描述,请参阅维基百科),而是正常的静态多变的。
如果您想使用更多静态变量,您可以考虑使用它,因为它使事情变得更加美观。
但是,要回答您的具体问题,您可以使用静态块来初始化这些变量,或者在减速后直接初始化它们。

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();
   }
}

或者

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();
   }
}

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();
   }
}
梦在深巷 2024-11-05 12:07:12
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>>>();
}
}
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>>>();
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文