intent传对象


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])即可。估计本方法是供外部类反序列化本类数组使用。
 
  1 package com.yingjie.jzlfapp.model.local;

  2 

  3 import android.os.Parcel;

  4 import android.os.Parcelable;

  5 

  6 /**

  7  * 类名称: Dede_MemberAnswer 功能描述:回答的视图 作者: chrizz00 创建日期: 2014-1-23 下午2:04:50

  8  */

  9 public class PersonCenterAnswer implements Parcelable {

 10 

 11 

 12     private Integer tid;

 13     // 回答问题类型

 14     private String tidname;

 15     

 16     // 回答者id

 17     private Integer replierid;

 18     // 回答者

 19     private String replier;

 20     

 21     //问题的id

 22     private Integer questionid;

 23     //问题

 24     private String title;

 25     

 26     //提问者的id

 27     private Integer askerid;

 28     //提问者

 29     private String asker;

 30     

 31     //提问的时间

 32     private long asktime;

 33     //回答的时间

 34     private long answertime;

 35     //解决的时间

 36     private long solvetime;

 37     

 38     // 被采纳的回答的id

 39     private Integer bestanswer;

 40     

 41     //回答的id

 42     private Integer answerid;

 43     //回答的内容

 44     private String content;

 45 

 46     public PersonCenterAnswer() {

 47 

 48     }

 49 

 50     public PersonCenterAnswer(Integer tid, String tidname, Integer replierid,

 51             String replier, Integer questionid, String title, Integer askerid,

 52             String asker, long asktime, long answertime, long solvetime,

 53             Integer bestanswer, Integer answerid, String content) {

 54         super();

 55         this.tid = tid;

 56         this.tidname = tidname;

 57         this.replierid = replierid;

 58         this.replier = replier;

 59         this.questionid = questionid;

 60         this.title = title;

 61         this.askerid = askerid;

 62         this.asker = asker;

 63         this.asktime = asktime;

 64         this.answertime = answertime;

 65         this.solvetime = solvetime;

 66         this.bestanswer = bestanswer;

 67         this.answerid = answerid;

 68         this.content = content;

 69     }

 70     

 71     public Integer getTid() {

 72         return tid;

 73     }

 74 

 75     public void setTid(Integer tid) {

 76         this.tid = tid;

 77     }

 78 

 79     public String getTidname() {

 80         return tidname;

 81     }

 82 

 83     public void setTidname(String tidname) {

 84         this.tidname = tidname;

 85     }

 86 

 87     public Integer getReplierid() {

 88         return replierid;

 89     }

 90 

 91     public void setReplierid(Integer replierid) {

 92         this.replierid = replierid;

 93     }

 94 

 95     public String getReplier() {

 96         return replier;

 97     }

 98 

 99     public void setReplier(String replier) {

100         this.replier = replier;

101     }

102 

103     public Integer getQuestionid() {

104         return questionid;

105     }

106 

107     public void setQuestionid(Integer questionid) {

108         this.questionid = questionid;

109     }

110 

111     public String getTitle() {

112         return title;

113     }

114 

115     public void setTitle(String title) {

116         this.title = title;

117     }

118 

119     public Integer getAskerid() {

120         return askerid;

121     }

122 

123     public void setAskerid(Integer askerid) {

124         this.askerid = askerid;

125     }

126 

127     public String getAsker() {

128         return asker;

129     }

130 

131     public void setAsker(String asker) {

132         this.asker = asker;

133     }

134 

135     public long getAsktime() {

136         return asktime;

137     }

138 

139     public void setAsktime(long asktime) {

140         this.asktime = asktime;

141     }

142 

143     public long getAnswertime() {

144         return answertime;

145     }

146 

147     public void setAnswertime(long answertime) {

148         this.answertime = answertime;

149     }

150 

151     public long getSolvetime() {

152         return solvetime;

153     }

154 

155     public void setSolvetime(long solvetime) {

156         this.solvetime = solvetime;

157     }

158 

159     public Integer getBestanswer() {

160         return bestanswer;

161     }

162 

163     public void setBestanswer(Integer bestanswer) {

164         this.bestanswer = bestanswer;

165     }

166 

167     public Integer getAnswerid() {

168         return answerid;

169     }

170 

171     public void setAnswerid(Integer answerid) {

172         this.answerid = answerid;

173     }

174 

175     public String getContent() {

176         return content;

177     }

178 

179     public void setContent(String content) {

180         this.content = content;

181     }

182 

183     

184     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     }

201     

202     

203     @Override

204     public int describeContents() {

205         return 0;

206     }

207 

208     @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     }

225 

226       public static final Parcelable.Creator<PersonCenterAnswer> CREATOR = new Parcelable.Creator<PersonCenterAnswer>(){

227 

228         @Override

229         public PersonCenterAnswer createFromParcel(Parcel source) {

230             

231             return new PersonCenterAnswer(source);

232         }

233 

234         @Override

235         public PersonCenterAnswer[] newArray(int size) {

236             

237             return new PersonCenterAnswer[size];

238         }

239           

240           

241       };

242 

243     @Override

244     public String toString() {

245         return "PersonCenterAnswer [tid=" + tid + ", tidname=" + tidname

246                 + ", replierid=" + replierid + ", replier=" + replier

247                 + ", questionid=" + questionid + ", title=" + title

248                 + ", askerid=" + askerid + ", asker=" + asker + ", asktime="

249                 + asktime + ", answertime=" + answertime + ", solvetime="

250                 + solvetime + ", bestanswer=" + bestanswer + ", answerid="

251                 + answerid + ", content=" + content + "]";

252     }

253 

254       

255     

256 }

writeToParcel (Parcel dest, int flags) 和PersonCenterAnswer(Parcel source)方法体里面的write和read的顺序要一一对应,不能出错!!切记!!

传输的时候直接intent.putExtra("answers", answerss);    (answers=PersonCenterAnswer)

在接收端直接  PersonCenterAnswer person =(PersonCenterAnswer) getIntent().getParcelableExtra("answers");

再次注意:这个person只是一个复制品,并不是传输前的那个对象。

如果需要同步,可以考虑将这个对象回传回去,在替换成先前的那个对象,试过可行,稍麻烦。

你可能感兴趣的:(intent)