creating parcelable in android from some of the fields of a class

后端 未结 2 1966
情歌与酒
情歌与酒 2021-01-27 19:26

i have the following class which i intent to pass from one activity to another:

public class Ad extends ListItem implements parcelable{
    private String _type;         


        
相关标签:
2条回答
  • 2021-01-27 19:58

    Try design your class like this..

    public class Form implements Parcelable {
    
    private String formdata1;
    private String formdata2;
    private String formdata3;
    private String formdata4;
    
    public Form() {
    }
    
    public Form(Parcel in) {
        setFormdata1(in.readString());
        setFormdata2(in.readString());
        setFormdata3(in.readString());
        setFormdata4(in.readString());
    }
    
    public String getFormdata1() {
        return formdata1;
    }
    
    public void setFormdata1(String formdata1) {
        this.formdata1 = formdata1;
    }
    
    public String getFormdata2() {
        return formdata2;
    }
    
    public void setFormdata2(String formdata2) {
        this.formdata2 = formdata2;
    }
    
    public String getFormdata3() {
        return formdata3;
    }
    
    public void setFormdata3(String formdata3) {
        this.formdata3 = formdata3;
    }
    
    public String getFormdata4() {
        return formdata4;
    }
    
    public void setFormdata4(String formdata4) {
        this.formdata4 = formdata4;
    }
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel in, int arg1) {
        in.writeString(getFormdata1());
        in.writeString(getFormdata2());
        in.writeString(getFormdata3());
        in.writeString(getFormdata4());
    }
    
    public static final Parcelable.Creator<Form> CREATOR = new Parcelable.Creator<Form>() {
    
        @Override
        public Form createFromParcel(Parcel in) {
            return new Form(in);
        }
    
        @Override
        public Form[] newArray(int size) {
            return new Form[size];
        }
    
    };
    

    }

    0 讨论(0)
  • 2021-01-27 20:16

    It's your code that writes to Parcel and your code that reads from Parcel. So basically yes. You can write whatever you want. Content of all members, content of some, no members, but other values you use to restore state of the object etc, etc.

    0 讨论(0)
提交回复
热议问题