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

后端 未结 2 1965
情歌与酒
情歌与酒 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
    CREATOR = new Parcelable.Creator() { @Override public Form createFromParcel(Parcel in) { return new Form(in); } @Override public Form[] newArray(int size) { return new Form[size]; } };

    }

提交回复
热议问题