0

I have the following two classes.

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;
            }
}

and

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;
    }

}

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.

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.

Ravi Kumar Seth
  • 154
  • 2
  • 14

5 Answers5

1

You can make your class implement Serializable and then while starting the activity do as

yourIntent.putExtra("myObj", obj);

and in the other activity:

getIntent().getSerializableExtra("myObj");

How to pass an object from one activity to another on Android

Community
  • 1
  • 1
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
1

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

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

Initialize singleton object in first activity and set arraylist value.

You can get the singleton class instance in other activity and get the arraylist which is already set by first activity.

Ex:

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();
Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25
0

Implement parcelable and then use putParcelableArrayListExtra from the intent like this:

Intent intent = new Intent(this, NextActivity.class);
intent.putParcelableArrayListExtra(name, arrayList);
startActivity(intent);

To get back the arraylist, use getIntent().getParcelableArrayListExtra(name). Make sure the two names match.

Jason Tang
  • 87
  • 2
  • 8
0
 public class People implements Parcelable {

         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;
        }

 private People(Parcel in) {
     name = in.readString();
     mobile_number = in.readInt();
     email_id = in.readString();
     testimony = in.readString();

 }

 @Override
 public int describeContents() {
     // TODO Auto-generated method stub
     return 0;
 }

 @Override
 public void writeToParcel(Parcel dest, int flags) {

     dest.writeString(name);
     dest.writeInt(mobile_number);
     dest.writeString(email_id);
     dest.writeString(testimony);

 }

  public static final Parcelable.Creator<People> CREATOR = new Parcelable.Creator<People>() {
         public People createFromParcel(Parcel in) {
             return new People(in);
         }

         public People[] newArray(int size) {
             return new People[size];

         }
     };

    // all get , set method 
  }

and this get and set for your code:

 Intent intent = new Intent(this,DisplayContact.class);
 intent.putExtra("Contact_list", ContactLis);
 startActivity(intent);

second class:

 ArrayList<People> myList = getIntent().getParcelableExtra("Contact_list");
pamitha
  • 55
  • 1
  • 5
0

He you can implement parcelable interface in your custom class then you can pass arraylist of this class to anotheractivity

class Testimony implements Parcelable {

        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;
        }

        protected Testimony(Parcel in) {
           testimony   = in.readString();
            name = in.readString();
            id = in.readInt();

        }
        public String gettestimony() {
            return testimony;
        }

        public String getname() {
            return name;
        }

        public int getid() {
            return id;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(testimony);
            dest.writeString(name);
            dest.writeInt(id);
        }
        public static final Creator<Image> CREATOR = new Creator<Image>() {
            @Override
            public Image createFromParcel(Parcel in) {
                return new Image(in);
            }

            @Override
            public Image[] newArray(int size) {
                return new Image[size];
            }
        };
    }
Chirag Jain
  • 133
  • 9