This is my class that I'm trying to make it Parcelable, As you can see in code I Stuck in some places in code:
public class CommentModel implements Parcelable{
String COMMENT_CONTENT;
String COMMENT_ID;
UserModel COMMENT_ACTOR;
public CommentModel(String comment_content, String comment_id, UserModel comment_user){
this.COMMENT_CONTENT = comment_content;
this.COMMENT_ID = comment_id;
this.COMMENT_ACTOR = comment_user;
}
/*== constructor to rebuild object from the Parcel ==*/
public CommentModel(Parcel source) {
this.COMMENT_CONTENT = source.readString();
this.COMMENT_ID = source.readString();
// This is where I'm stuck!
this.COMMENT_ACTOR = source............;
}
public String getContent(){
return this.COMMENT_CONTENT;
}
public void setContent(String content){
this.COMMENT_CONTENT = content;
}
public String getId(){
return this.COMMENT_ID;
}
public void setId(String id){
this.COMMENT_ID = id;
}
public UserModel getActor(){
return this.COMMENT_ACTOR;
}
public void setActor(UserModel actor){
this.COMMENT_ACTOR = actor;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(COMMENT_CONTENT);
dest.writeString(COMMENT_ID);
// This is where I'm stuck too! :(
dest.write.....
}
public static final Parcelable.Creator<CommentModel> CREATOR = new Creator<CommentModel>() {
@Override
public CommentModel[] newArray(int size) {
return new CommentModel[size];
}
@Override
public CommentModel createFromParcel(Parcel source) {
return new CommentModel(source);
}
};
}
How should do it? Is It possible totally? if not how can handle this issue?
P.s: My UserModel() is Parcelable too of course.
if UserModel
is Parcelable
, you can use writeParcelable to write the object and readParcelable to read it back. E.g.
dest.writeParcelable(COMMENT_ACTOR, flages);
to write, and
COMMENT_ACTOR = source.readParcelable(UserModel.class.getClassLoader());
to read it back.