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

后端 未结 30 3952
遇见更好的自我
遇见更好的自我 2020-11-21 04:03

I am trying to work on sending an object of my customer class from one Activity and display it in another Activity.

The code for t

30条回答
  •  无人及你
    2020-11-21 04:57

    I am using parcelable to send data from one activity to another acivity. Here is my code that works fine in my project.

    public class Channel implements Serializable, Parcelable {
    
        /**  */
        private static final long serialVersionUID = 4861597073026532544L;
    
        private String cid;
        private String uniqueID;
        private String name;
        private String logo;
        private String thumb;
    
    
        /**
         * @return The cid
         */
        public String getCid() {
            return cid;
        }
    
        /**
         * @param cid
         *     The cid to set
         */
        public void setCid(String cid) {
            this.cid = cid;
        }
    
        /**
         * @return The uniqueID
         */
        public String getUniqueID() {
            return uniqueID;
        }
    
        /**
         * @param uniqueID
         *     The uniqueID to set
         */
        public void setUniqueID(String uniqueID) {
            this.uniqueID = uniqueID;
        }
    
        /**
         * @return The name
         */
        public String getName() {
            return name;
        }
    
        /**
         * @param name
         *            The name to set
         */
        public void setName(String name) {
            this.name = name;
        }
    
        /**
         * @return the logo
         */
        public String getLogo() {
            return logo;
        }
    
        /**
         * @param logo
         *     The logo to set
         */
        public void setLogo(String logo) {
            this.logo = logo;
        }
    
        /**
         * @return the thumb
         */
        public String getThumb() {
            return thumb;
        }
    
        /**
         * @param thumb
         *     The thumb to set
         */
        public void setThumb(String thumb) {
            this.thumb = thumb;
        }
    
    
        public Channel(Parcel in) {
            super();
            readFromParcel(in);
        }
    
        public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
            public Channel createFromParcel(Parcel in) {
                return new Channel(in);
            }
    
            public Channel[] newArray(int size) {
    
                return new Channel[size];
            }
        };
    
        public void readFromParcel(Parcel in) {
            String[] result = new String[5];
            in.readStringArray(result);
    
            this.cid = result[0];
            this.uniqueID = result[1];
            this.name = result[2];
            this.logo = result[3];
            this.thumb = result[4];
        }
    
        public int describeContents() {
            return 0;
        }
    
        public void writeToParcel(Parcel dest, int flags) {
    
            dest.writeStringArray(new String[] { this.cid, this.uniqueID,
                    this.name, this.logo, this.thumb});
        }
    }
    

    In activityA use it like this:

    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList("channel",(ArrayList) channels);
    Intent intent = new Intent(ActivityA.this,ActivityB.class);
    intent.putExtras(bundle);
    startActivity(intent);
    

    In ActivityB use it like this to get data:

    Bundle getBundle = this.getIntent().getExtras();
    List channelsList = getBundle.getParcelableArrayList("channel");
    

提交回复
热议问题