2016-04-15 6 views
1

経由Parcelableを置く:B活動に私は意図経由<strong></strong>活動から<strong>B</strong>活動にParcelableオブジェクトを渡すためにしようとしています意図

Intent intent = new Intent (A.this, B.class); 

intent.putExtra ("post", mypost); // where post implements Parcelable` 

私は、このようにポストオブジェクトを得た:

Post myPost = getIntent().getParcelableExtra("post"); 

Bで活性myPostオブジェクトフィールドが混合され、例えばポストモデルにpostTextフィールドとpostDateフィールドがあり、Bアクティビティのこのフィールドの値は混在しています。

なぜこのようなことが起こりますか?私のモデルクラスの外観は、次のことを好き:

publicクラスのポストはParcelable、シリアライズ{

private static final long serialVersionUID = 2L; 

@SerializedName("author") 
private User author; 

@SerializedName("comments_count") 
private String commentsCount; 

@SerializedName("image") 
private String imageToPost; 

@SerializedName("parent_key") 
private String parentKey; 

@SerializedName("created_date") 
private String postDate; 

@SerializedName("id") 
private String postId; 

@SerializedName("text") 
private String postText; 

@SerializedName("title") 
private String postTitle; 

@SerializedName("shared_post_id") 
private String sharedPostId; 

@SerializedName("url") 
private String urlToPost; 

@SerializedName("video") 
private String videoToPost; 

public Post() { 
} 

public Post(Parcel in) { 
    author = (User) in.readValue(getClass().getClassLoader()); 
    commentsCount = in.readString(); 
    imageToPost = in.readString(); 
    parentKey = in.readString(); 
    postDate = in.readString(); 
    postId = in.readString(); 
    postText = in.readString(); 
    postTitle = in.readString(); 
    sharedPostId = in.readString(); 
    urlToPost = in.readString(); 
    videoToPost = in.readString(); 
} 

public static final Creator<Post> CREATOR = new Creator<Post>() { 
    @Override 
    public Post createFromParcel(Parcel in) { 
     return new Post(in); 
    } 

    @Override 
    public Post[] newArray(int size) { 
     return new Post[size]; 
    } 
}; 

public User getAuthor() { 
    return author; 
} 

public void setAuthor(User author) { 
    this.author = author; 
} 

public String getPostDate() { 
    return postDate; 
} 

public void setPostDate(String postDate) { 
    this.postDate = postDate; 
} 

public String getPostTitle() { 
    return postTitle; 
} 

public void setPostTitle(String postTitle) { 
    this.postTitle = postTitle; 
} 

public String getPostText() { 
    return postText; 
} 

public void setPostText(String postText) { 
    this.postText = postText; 
} 

public String getPostId() { 
    return postId; 
} 

public void setPostId(String postId) { 
    this.postId = postId; 
} 

public String getUrlToPost() { 
    return urlToPost; 
} 

public void setUrlToPost(String urlToPost) { 
    this.urlToPost = urlToPost; 
} 

public String getImageToPost() { 
    return imageToPost; 
} 

public void setImageToPost(String imageToPost) { 
    this.imageToPost = imageToPost; 
} 

public String getVideoToPost() { 
    return videoToPost; 
} 

public void setVideoToPost(String videoToPost) { 
    this.videoToPost = videoToPost; 
} 

public String getParentKey() { 
    return parentKey; 
} 

public void setParentKey(String parentKey) { 
    this.parentKey = parentKey; 
} 

public String getCommentsCount() { 
    return commentsCount; 
} 

public void setCommentsCount(String commentsCount) { 
    this.commentsCount = commentsCount; 
} 

public String getSharedPostId() { 
    return sharedPostId; 
} 

public void setSharedPostId(String sharedPostId) { 
    this.sharedPostId = sharedPostId; 
} 

@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeValue(author); 
    dest.writeString(commentsCount); 
    dest.writeString(imageToPost); 
    dest.writeString(parentKey); 
    dest.writeString(postDate); 
    dest.writeString(postId); 
    dest.writeString(postText); 
    dest.writeString(postTitle); 
    dest.writeString(sharedPostId); 
    dest.writeString(urlToPost); 
    dest.writeString(videoToPost); 
} 

}

+0

あなたwriteToParcelですか?あなたのためにこのステップを自動化するプラグインがあります – Blackbelt

+0

これは私のGo-to Parceableプラグインですhttps://github.com/mcharmas/android-parcelable-intellij-plugin。あなたの 'writeToParcel()'には、あなたの項目が書かれた順番も混在している可能性があります。 –

+0

@Anil:Postクラスの完全なコードを投稿してください。私はそれが起こっていると思います。なぜなら、あなたは異なる順序で読み物を書いているからです。私はそれをより明確にすることができるように投稿してください。 –

答えて

0

がdescribeContentsとwriteToParcelを追加実装しています。

例:

@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(name); 
    dest.writeString(email); 
    dest.writeString(pass); 
    dest.writeFloat(amountPaid); 
    dest.writeString(url); 
    dest.writeInt(age); 
} 
+0

私はwriteParcel関数とdescribeContentsを持っています。私のモデルは正しい順序でパーセルを読み書きします: –

関連する問題