Parcelable 替换字段对象的值?

Posted

技术标签:

【中文标题】Parcelable 替换字段对象的值?【英文标题】:Parcelable replacing field object's values? 【发布时间】:2020-08-18 02:04:43 【问题描述】:

我正在将ArrayList<Contact> 中的Parcelable 对象从Activity1 发送到Activity2。

在我真正将对象发送到 Activity2 之前,我做了简单的Log.i() 函数。它打印出来:

I/String representation from Activity1: Contactid=1, firstName='John', lastName='Doe', email='john@gmail.com', photoPath='/storage/emulated/0/Pictures/Screenshots/Screenshot_20200502-094346__02.jpg', note='some notes'

在 Activity2 中,它打印出以下内容:

I/String representation from Activity2: Contactid=6, firstName='1', lastName='John', email='Doe', photoPath='john@gmail.com', note='/storage/emulated/0/Pictures/Screenshots/Screenshot_20200502-094346__02.jpg'

对象完全搞砸了。 id 不可能为 6,因为我在 db 中只有一个对象。其他字段可以自己查看。

最后,这里是完整的Contact类:

public class Contact implements Parcelable

    @SerializedName("id")
    @Expose
    private Integer id;

    @SerializedName("firstName")
    @Expose
    private String firstName;

    @SerializedName("lastName")
    @Expose
    private String lastName;

    @SerializedName("email")
    @Expose
    private String email;

    @SerializedName("photoPath")
    @Expose
    private String photoPath;

    @SerializedName("note")
    @Expose
    private String note;


    public Contact()
        //photoPath = "";
    

    public Contact(Integer id, String firstName, String lastName, String email, String photoPath, String note) 
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.photoPath = photoPath;
        this.note = note;
    

    protected Contact(Parcel in) 
        id = in.readInt();
        firstName = in.readString();
        lastName = in.readString();
        email = in.readString();
        photoPath = in.readString();
        note = in.readString();

    

    public Integer getId() 
        return id;
    

    public void setId(Integer id) 
        this.id = id;
    

    public String getFirstName() 
        return firstName;
    

    public void setFirstName(String firstName) 
        this.firstName = firstName;
    

    public String getLastName() 
        return lastName;
    

    public void setLastName(String lastName) 
        this.lastName = lastName;
    

    public String getEmail() 
        return email;
    

    public void setEmail(String email) 
        this.email = email;
    

    public String getPhotoPath() 
        return photoPath;
    

    public void setPhotoPath(String photoPath) 
        this.photoPath = photoPath;
    

    public String getNote() 
        return note;
    

    public void setNote(String note) 
        this.note = note;
    

    public String getPhotoFilename() 
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        // Log.i("timestampe", timeStamp);
        return "IMG_" + getId() + "_" + timeStamp + ".jpg";
    

    @Override
    public int describeContents() 
        return 0;
    

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

        dest.writeStringArray(new String[]this.id.toString(), this.firstName, this.lastName, this.email, this.photoPath, this.note);

        /*dest.writeString(this.id.toString());
        dest.writeString(this.firstName);
        dest.writeString(this.lastName);
        dest.writeString(this.email);
        dest.writeString(this.photoPath);
        dest.writeString(this.note);*/
    

    public static final Parcelable.Creator<Contact> CREATOR= new Parcelable.Creator<Contact>() 
        @Override
        public Contact createFromParcel(Parcel source) 
             // TODO Auto-generated method stub
            return new Contact(source);  //using parcelable constructor
        

        @Override
        public Contact[] newArray(int size) 
            // TODO Auto-generated method stub
            return new Contact[size];
        

    ;

    @Override
    public String toString() 
        return "Contact" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", photoPath='" + photoPath + '\'' +
                ", note='" + note + '\'' +
                '';
    

from writeToParcel() 这两个选项我都试过了,都没用。

【问题讨论】:

您的writeToParcel 都不匹配您的Contact(Parcel in) 构造函数。你在哪里writeInt() 匹配你的readInt() 啊,我想我明白了。在 protected Contact(Parcel in) 中应该有 id = in.readInt() 并且在 writeToParcel(Parcel dest, int flags) 中应该有 dest.writeInt(this.id)。 【参考方案1】:

您的writeToParcel() 需要与您的Contact(Parcel in) 完全匹配:如果您使用readInt(),则需要以完全相同的顺序使用writeInt()

【讨论】:

【参考方案2】:

是的,伊恩,我确实喜欢你在评论中提出的建议。现在可以了。谢谢你:)

protected Contact(Parcel in) 
    id = in.readInt();
    firstName = in.readString();
    lastName = in.readString();
    email = in.readString();
    photoPath = in.readString();
    note = in.readString();



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

    dest.writeInt(this.id);
    dest.writeString(this.firstName);
    dest.writeString(this.lastName);
    dest.writeString(this.email);
    dest.writeString(this.photoPath);
    dest.writeString(this.note);

【讨论】:

以上是关于Parcelable 替换字段对象的值?的主要内容,如果未能解决你的问题,请参考以下文章

***Error关于为android中的类编写Parcelable

Parcelable 在写入可序列化对象时遇到 IOException ......?

替换 Parcelable 接口的最佳实践

Parcelable遇到IOException编写可序列化对象…?

Parcelable, Serializable,Cloneable,copyProperties

如何写Parcelable()一个实现Serializable的类型的字段