[英]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. 以mInstance
为null
并且由于没有公共构造函数,因此不可能使用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.