我有以下两个类(class)。
public class People{
private int mobile_number;
private String email_id, name;
private Testimony[] testimony;
public People(String name, int mobile_number, String email_id, Testimony ... testimony) {
this.name = name;
this.mobile_number = mobile_number;
this.email_id = email_id;
this.testimony = testimony;
}
public Testimony[] getTestimony() {
return testimony;
}
public String getEmail_id() {
return email_id;
}
public int getMobile_number() {
return mobile_number;
}
public String getName() {
return name;
}
}
和
public class Testimony {
private String testimony;
private String name;
private int id;
public Testimony(String testimony,String name, int id){
this.testimony = testimony;
this.id = id;
this.name = name;
}
public String gettestimony() {
return testimony;
}
public String getname() {
return name;
}
public int getid() {
return id;
}
}
ArrayList myPeople 中有很多对象,我碰巧知道我需要使用哪个 ArrayList 项目。考虑到 Testimony ArrayList 具有可变数量的对象,有时可能具有大量对象,将特定人员对象从一个 Activity 传递到另一个 Activity 的最佳方式是什么。
有another question读起来很相似,但实际上是不同的,因为另一个问题解决了如何将一个对象传递给另一个Android Activity,而不是一个数组对象强>.
最佳答案
虽然您可以使用序列化方法将数组列表从一个 Activity 传递到另一个 Activity ,但我觉得这不是好方法。
相反,您创建一个 Singleton 类以及 setter 和 getter 方法来设置和获取数组列表。
在第一个 Activity 中初始化单例对象并设置数组列表值。
您可以在其他 Activity 中获取单例类实例,并获取第一个 Activity 已设置的数组列表。
例如:
public class UtilityClass {
private static UtilityClass instance;
private ArrayList list;
public ArrayList getList() {
return list;
}
public void setList(ArrayList list) {
this.list = list;
}
private UtilityClass(){}
public static UtilityClass getInstance(){
if(instance == null){
instance = new UtilityClass();
}
return instance;
}
}
您可以从第一个 Activity 开始设置,如下所示,
UtilityClass.getInstance().setList(list);
从第二个 Activity 中获取此信息,如下所示,
UtilityClass.getInstance().getList();
关于java - 将 Array<Object> 从一个 Activity 传递到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36417251/