传递Array< Object>从一项活动到另一项活动 [英] Passing Array<Object> from one activity to another

查看:68
本文介绍了传递Array< Object>从一项活动到另一项活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个课程.

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项.考虑到证言ArrayList"具有可变数量的对象,有时可以具有大量对象的事实,将某个特定对象从一个活动"传递到另一个活动"的最佳方法是什么.

There are many objects in the ArrayList myPeople and I happen to know which ArrayList item i need to use. What can be the best way of passing a particular people object from one Activity to another considering the fact that the Testimony ArrayList has variable number of objects and sometimes can have large number of objects.

还有另一个问题读起来类似,但实际上却有所不同,因为另一个所说的问题解决了如何将对象传递给另一个Android Activity,而不是如何将 array 传递给对象.

There is another question that reads similar, but it is actually different, because the other said question addresses how to pass an object to another Android Activity rather than an array of objects.

推荐答案

尽管您可以使用序列化方法将arraylist从一个活动传递到另一个活动,但我觉得这不是一个好方法.

Though you can pass arraylist from one activity to other using serialize method, i feel this is not good way.

您将创建一个Singleton类以及setter和getter方法来设置和获取arraylist.

Instead you create one Singleton class and setter and getter methods to set and get arraylist.

在第一个活动中初始化单例对象并设置arraylist值.

Initialize singleton object in first activity and set arraylist value.

您可以在其他活动中获取单例类实例,并获取第一个活动已设置的arraylist.

You can get the singleton class instance in other activity and get the arraylist which is already set by first 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;
       }
}

您可以从下面的第一个活动中进行设置,

You can set from first activity like below,

UtilityClass.getInstance().setList(list);

从下面的第二个活动中获取此信息,

Get this from second activity like below,

UtilityClass.getInstance().getList();

这篇关于传递Array< Object>从一项活动到另一项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆