intent还有一个很好用的地方,就是传输对象,但要注意的是这里的传输只是将对象复制了一份通过intent进行传递,并不能达到实时更新的效果,也就是这个对象等偏重于“读”。intent对这个对象有着严格的要求----Parcel。
android提供了一种新的类型:Parcel。本类被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以
外,只有实现了Parcelable接口的类才能被放入Parcel中。
Parcelable实现要点:需要实现三个东西
1)writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中.声明如下:
writeToParcel (Parcel dest, int flags) 具体参数含义见javadoc
2)describeContents方法。没搞懂有什么用,反正直接返回0也可以
3)静态的Parcelable.Creator接口,本接口有两个方法:
createFromParcel(Parcel in) 实现从in中创建出类的实例的功能
newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。估计本方法是供外部类反序列化本类数组使用。
package com.yingjie.jzlfapp.model.local; import android.os.Parcel;
import android.os.Parcelable; /**
* 类名称: Dede_MemberAnswer 功能描述:回答的视图 作者: chrizz00 创建日期: 2014-1-23 下午2:04:50
*/
public class PersonCenterAnswer implements Parcelable { private Integer tid;
// 回答问题类型
private String tidname; // 回答者id
private Integer replierid;
// 回答者
private String replier; //问题的id
private Integer questionid;
//问题
private String title; //提问者的id
private Integer askerid;
//提问者
private String asker; //提问的时间
private long asktime;
//回答的时间
private long answertime;
//解决的时间
private long solvetime; // 被采纳的回答的id
private Integer bestanswer; //回答的id
private Integer answerid;
//回答的内容
private String content; public PersonCenterAnswer() { } public PersonCenterAnswer(Integer tid, String tidname, Integer replierid,
String replier, Integer questionid, String title, Integer askerid,
String asker, long asktime, long answertime, long solvetime,
Integer bestanswer, Integer answerid, String content) {
super();
this.tid = tid;
this.tidname = tidname;
this.replierid = replierid;
this.replier = replier;
this.questionid = questionid;
this.title = title;
this.askerid = askerid;
this.asker = asker;
this.asktime = asktime;
this.answertime = answertime;
this.solvetime = solvetime;
this.bestanswer = bestanswer;
this.answerid = answerid;
this.content = content;
} public Integer getTid() {
return tid;
} public void setTid(Integer tid) {
this.tid = tid;
} public String getTidname() {
return tidname;
} public void setTidname(String tidname) {
this.tidname = tidname;
} public Integer getReplierid() {
return replierid;
} public void setReplierid(Integer replierid) {
this.replierid = replierid;
} public String getReplier() {
return replier;
} public void setReplier(String replier) {
this.replier = replier;
} public Integer getQuestionid() {
return questionid;
} public void setQuestionid(Integer questionid) {
this.questionid = questionid;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public Integer getAskerid() {
return askerid;
} public void setAskerid(Integer askerid) {
this.askerid = askerid;
} public String getAsker() {
return asker;
} public void setAsker(String asker) {
this.asker = asker;
} public long getAsktime() {
return asktime;
} public void setAsktime(long asktime) {
this.asktime = asktime;
} public long getAnswertime() {
return answertime;
} public void setAnswertime(long answertime) {
this.answertime = answertime;
} public long getSolvetime() {
return solvetime;
} public void setSolvetime(long solvetime) {
this.solvetime = solvetime;
} public Integer getBestanswer() {
return bestanswer;
} public void setBestanswer(Integer bestanswer) {
this.bestanswer = bestanswer;
} public Integer getAnswerid() {
return answerid;
} public void setAnswerid(Integer answerid) {
this.answerid = answerid;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public PersonCenterAnswer(Parcel source) {
185
186 this.tid = source.readInt();
187 this.tidname = source.readString();
188 this.replierid = source.readInt();
189 this.replier = source.readString();
190 this.questionid =source.readInt();
191 this.title = source.readString();
192 this.askerid = source.readInt();
193 this.asker = source.readString();
194 this.asktime = source.readLong();
195 this.answertime = source.readLong();
196 this.solvetime = source.readLong();
197 this.bestanswer = source.readInt();
198 this.answerid =source.readInt();
199 this.content = source.readString();
200 } @Override
public int describeContents() {
return 0;
} @Override
209 public void writeToParcel(Parcel dest, int flags) {
210 dest.writeInt(tid);
211 dest.writeString(tidname);
212 dest.writeInt(replierid);
213 dest.writeString(replier);
214 dest.writeInt(questionid);
215 dest.writeString(title);
216 dest.writeInt(askerid);
217 dest.writeString(asker);
218 dest.writeLong(asktime);
219 dest.writeLong(answertime);
220 dest.writeLong(solvetime);
221 dest.writeInt(bestanswer);
222 dest.writeInt(answerid);
223 dest.writeString(content);
224 } public static final Parcelable.Creator<PersonCenterAnswer> CREATOR = new Parcelable.Creator<PersonCenterAnswer>(){ @Override
public PersonCenterAnswer createFromParcel(Parcel source) { return new PersonCenterAnswer(source);
} @Override
public PersonCenterAnswer[] newArray(int size) { return new PersonCenterAnswer[size];
} }; @Override
public String toString() {
return "PersonCenterAnswer [tid=" + tid + ", tidname=" + tidname
+ ", replierid=" + replierid + ", replier=" + replier
+ ", questionid=" + questionid + ", title=" + title
+ ", askerid=" + askerid + ", asker=" + asker + ", asktime="
+ asktime + ", answertime=" + answertime + ", solvetime="
+ solvetime + ", bestanswer=" + bestanswer + ", answerid="
+ answerid + ", content=" + content + "]";
} }
writeToParcel (Parcel dest, int flags) 和PersonCenterAnswer(Parcel source)方法体里面的write和read的顺序要一一对应,不能出错!!切记!!
传输的时候直接intent.putExtra("answers", answerss); (answers=PersonCenterAnswer)
在接收端直接 PersonCenterAnswer person =(PersonCenterAnswer) getIntent().getParcelableExtra("answers");
再次注意:这个person只是一个复制品,并不是传输前的那个对象。
如果需要同步,可以考虑将这个对象回传回去,在替换成先前的那个对象,试过可行,稍麻烦。