问题描述
我试图从一个活动传递完整的ArrayList到另一个。
I am trying to pass the complete arraylist from one activity to another.
我曾尝试这样的方式..
i have tried like this way..
arraylist=new ArrayList<HashMap<String,Object>>();
Intent i= new Intent(ListActivity.this,search.class);
i.putExtra("arraylist", arraylist);
startActivity(i);
有人能帮助我@thanks
Could somebody help me out @thanks
推荐答案
这是行不通的,因为在Java中对象
类是不可序列。见this问题一个解释为什么。
This will not work because the Object
class in Java is not serializable. See this question for an explanation as to why.
的 Intent.putExtra()
方法需要实现Serializable接口类型,对象不那么因此它不会工作,实现这一点。我建议,而不是一个的HashMap&LT;弦乐,对象&gt;
您使用实现Serializable接口的一个更具体的类型替换对象。请参阅如何做到这一点这。
The Intent.putExtra()
method requires a type that implements the serializable interface, Object does not implement this so consequently it will not work. I would suggest rather than having a HashMap<String,Object>
you replace the Object with a more specific type that implements the Serializable interface. See this tutorial for how to do this.
更新
如果您传递大量数据,则可能是序列化和反序列化相关的一个相当显著的开销。因此它可能会使用静态Singleton类来存储数组列表是值得的。在code下面的示例展示了如何实现这一点:
If the data you are passing is large there could be a fairly significant overhead associated with serializing and deserializing. Consequently it might be worth using a Static Singleton class to store the arraylist. The code sample below shows how you could implement this:
public class DataStore {
private static final DataStore instance = new DataStore ();
private arraylist = new ArrayList<HashMap<String,Object>>();
//Private constructor
private DataStore () {}
//Class is only accessible through this method
public static Singleton getInstance() {
return instance;
}
//Accessors for your data
private ArrayList<HashMap<String,Object>> getArrayList()
{
return arraylist;
}
private void setArrayList(ArrayList<HashMap<String,Object>> value)
{
arraylist = value;
}
}
有关引用这里是一个上静态单身。
For reference here is a tutorial on static singletons.
这篇关于从一个活动传递到另一个ArrayList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!