简体   繁体   English

如何在Android中保存全局“列表”或至少将其实例化以用于任何活动?

[英]How I can save a “list” of globally or at least instance it for use in any activity, in Android?

I create a class called Singleton which contains a list and instance 我创建一个名为Singleton的类,其中包含一个列表和实例

public class  Singleton {
private static Singleton instance;
private List<MaterialModel> list = new ArrayList<MaterialModel>();

public Singleton(){
}

public List<MaterialModel> getList() {
    return list;
}

public void setList(List<MaterialModel> list) {
    this.list = list;
}

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

public void addElement(MaterialModel material){
    this.list.add(material);
}

public MaterialModel getElement(int pos){
    return this.list.get(pos);
}

public boolean foundElement(MaterialModel material){
    if(this.list.contains(material)){
        return true;
    }
    return false;
}


}

The idea is to store items on the list and get them in any activity. 想法是将项目存储在列表中,并在任何活动中获取它们。 In reality I'm trying to get this list can be used in any kind and do not lose items within it. 实际上,我正在尝试使此列表可以以任何形式使用,并且不会丢失其中的项目。

First activity 第一次活动

public class FirstActivity extends SherlockActivity{
    Singleton singleton;
        ...
       public onCreate(...){
       singleton.addElement(obj)); // MaterialModel obj = new MaterialModel(.....)

Second Activty 第二活动

public class SecondActivity extends SherlockActivity{
        Singleton singleton;
            ...
           public onCreate(...){
           MaterialModel tmp = singleton.getElement(0)); // getElement return "obj"

The problem is that does not work, how can I fix it or at least save the list globaly so that it can be used in any activity ? 问题是,该方法不起作用,我该如何修复它或至少全局保存列表,以便可以在任何活动中使用它

You need to get the instance first of your Singleton before modifying any data in it. 您需要先获取Singleton实例,然后再修改其中的任何数据。

example: 例:

every time you call the Singleton singleton you need to call the static instance of it and instantiate in your Singleton object; 每次调用Singleton singleton都需要调用它的静态实例并在Singleton对象中实例化;

Singleton singleton = Singleton.getInstance();

I don't think you understand the idea behind the 'singleton' pattern. 我认为您不了解“单一”模式背后的想法。

Firstly, don't create a public constructor - instead (as Rod_Algonquin) suggests you need to use a static method (usually called getInstance ) to return the single instance of the singleton. 首先,不要创建public构造函数-相反(如Rod_Algonquin所示)建议您需要使用static方法(通常称为getInstance )来返回单例的单个实例。

Example... 例...

public class Singleton {
    private static Singleton mInstance = null;

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

    // Other code here

}

In the above code the singleton class holds a reference to a single instance of itself as mInstance . 在上面的代码中,singleton类mInstance自身的单个实例的引用称为mInstance To start with mInstance is null and as there is no public constructor, it's not possible to create a new instance of the singleton from another class by using new Singleton() but it is possible from within the singleton itself. mInstancenull并且由于没有公共构造函数,因此不可能使用new Singleton()从另一个类创建单例的新实例,但可以从单例内部进行。

Instead, whenever you need to access your singleon use... 相反,每当您需要访问单例使用时...

Singleton.getInstance();

If you look at the code for getInstance() you'll see... 如果查看getInstance()的代码,将会看到...

if (mInstance == null)
    mInstance = new Singleton();
return mInstance;

Obviously the first time getInstance() is called mInstance will be null so the if condition will be true and the line mInstance = new Singleton() will be executed. 显然,第一次调用getInstance() mInstance将为null因此if条件为true且将执行mInstance = new Singleton()行。 Following that, the method will return the instance referenced by mInstance . 之后,该方法将返回mInstance引用的实例。

On any subsequent calls to Singleton.getInstance() from any class or method in your code, mInstance will already be instantiated and therefore the if condition will be false but the getInstance method will still return mInstance as the single instance of the singleton. 从代码中的任何类或方法对Singleton.getInstance()任何后续调用中, mInstance将已经实例化,因此if条件将为false,但getInstance方法仍将mInstance作为单例的单个实例返回。

As an added bonus you can either hold a reference to the singleton in your code and call a method such as... 另外,您可以在代码中保留对单例的引用,然后调用诸如...的方法。

Singleton theSingleton = Singleton.getInstance();
theSingleton.doSomething();

...or you can simply use it to chain a call to a public method in the singleton such as... ...或者您可以简单地使用它将调用链接到单例中的公共方法,例如...

Singleton.getInstance().doSomething();

So for your first Activity you can do this... 因此,对于您的第一个Activity您可以执行此操作...

Singleton.getInstance().addElement(obj);

...and for your second Activity do this... ...在您的第二个Activity执行此操作...

MaterialModel tmp = Singleton.getInstance().getElement(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android应用程序中全局访问另一个班级的活动? - How can I access an Activity from another class globally in an Android app? ANDROID - 如何保存字符串并从任何应用程序访问它 - ANDROID - How can I save a string and access it from any App 如何保存列表实例<fragments></fragments> - How do I save instance of List<Fragments> 如何使用onRestart()取消暂停/重新启动Android Activity中的线程? - How can I use onRestart() to unpause/restart a thread in an Android Activity? Android:可打包对象已成功传递给另一个活动,但现在我无法使用其任何方法 - Android: Parcelable object successfully passed to another activity, but now I can't use any of its methods 如何加载文件并将其保存在Android中的对象列表中? - How I can load a file and save this in a list of objects in Android? Baseadapter中的Android Listview如何保存对列表的更改 - Android Listview in Baseadapter how can I save changes to the list 如何从活动中访问方法并将其用于Android中的另一个活动? - How can I access a method from an activity and use it into another activity in Android? 如何将Activity Class另存为变量以供以后在Android中使用 - How to save Activity Class as a variable to use it later in android 如何在 android 活动中使用多个 inheritance? - How can use multiple inheritance in android activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM