How to send an object from one Android Activity to another using Intents?

后端 未结 30 3681
-上瘾入骨i
-上瘾入骨i 2020-11-21 04:47

How can I pass an object of a custom type from one Activity to another using the putExtra() method of the class Intent?

30条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 05:13

    public class SharedBooking implements Parcelable{
    
        public int account_id;
        public Double betrag;
        public Double betrag_effected;
        public int taxType;
        public int tax;
        public String postingText;
    
        public SharedBooking() {
            account_id = 0;
            betrag = 0.0;
            betrag_effected = 0.0;
            taxType = 0;
            tax = 0;
            postingText = "";
        }
    
        public SharedBooking(Parcel in) {
            account_id = in.readInt();
            betrag = in.readDouble();
            betrag_effected = in.readDouble();
            taxType = in.readInt();
            tax = in.readInt();
            postingText = in.readString();
        }
    
        public int getAccount_id() {
            return account_id;
        }
        public void setAccount_id(int account_id) {
            this.account_id = account_id;
        }
        public Double getBetrag() {
            return betrag;
        }
        public void setBetrag(Double betrag) {
            this.betrag = betrag;
        }
        public Double getBetrag_effected() {
            return betrag_effected;
        }
        public void setBetrag_effected(Double betrag_effected) {
            this.betrag_effected = betrag_effected;
        }
        public int getTaxType() {
            return taxType;
        }
        public void setTaxType(int taxType) {
            this.taxType = taxType;
        }
        public int getTax() {
            return tax;
        }
        public void setTax(int tax) {
            this.tax = tax;
        }
        public String getPostingText() {
            return postingText;
        }
        public void setPostingText(String postingText) {
            this.postingText = postingText;
        }
        public int describeContents() {
            // TODO Auto-generated method stub
            return 0;
        }
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(account_id);
            dest.writeDouble(betrag);
            dest.writeDouble(betrag_effected);
            dest.writeInt(taxType);
            dest.writeInt(tax);
            dest.writeString(postingText);
    
        }
    
        public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
        {
            public SharedBooking createFromParcel(Parcel in)
            {
                return new SharedBooking(in);
            }
            public SharedBooking[] newArray(int size)
            {
                return new SharedBooking[size];
            }
        };
    
    }
    

    Passing the data:

    Intent intent = new Intent(getApplicationContext(),YourActivity.class);
    Bundle bundle = new Bundle();
    i.putParcelableArrayListExtra("data", (ArrayList) dataList);
    intent.putExtras(bundle);
    startActivity(intent);
    

    Retrieving the data:

    Bundle bundle = getIntent().getExtras();
    dataList2 = getIntent().getExtras().getParcelableArrayList("data");
    

提交回复
热议问题