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只是一个复制品,并不是传输前的那个对象。

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

intent传对象的更多相关文章

  1. 关于 android Intent 传对象和对象数组的一些操作

    直接开正题,Intent传递值就是平常那些很简单的,接下来介绍传递 对象,和 对象数组 1 .intent 传递自定义的 对象 - 实体类继承  Serializable public class A ...

  2. intent传值传对象跳转

    intent传值传对象跳转 1.传值 //原activity中存入一个字段 intent = new Intent(From.this, To.class); intent.putExtra(&quo ...

  3. Intent 传数据

    Intent作为android重要的组件其重要性不言而喻,这里说说他是怎么传递简单数据和对象 Intent的具体概念就不讲解了!网上有很多的 传递简单的数据(例如String,float等) 传递对象 ...

  4. android intent 传数据

    1. 基本数据类型 Intent intent = new Intent(); intent.setClass(activity1.this, activity2.class); //描述起点和目标 ...

  5. android中用Intent传数据,如果用传递的是一个类,就将类实现Parcelable接口

    Parcelable,内存单位,跨进程使用,或者intent传递对象的时候使用.android中用Intent传数据,如果用传递的是一个对象,就将对象实现Parcelable接口,而不是将对象序列化. ...

  6. Intent传递对象的几种方式

    原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/51105060 李济洲的博客 Intent的使用方法相信你已经比較熟悉了,Inte ...

  7. Android 全局获取 Context 与使用 Intent 传递对象

    =====================全局获取 Context======================== Android 开发中很多地方需要用到 Context,比如弹出 Toast.启动活 ...

  8. Android开发——使用intent传递对象

    intent传递对象有两种方法: 方式一:Serializable 方式 方式二:Parcelable方式 在这里不多介绍了,这一篇就是快速上手使用教程,至于详细原理介绍的,请看这一篇http://w ...

  9. onclick传对象

    用onclick传对象的时候,用jquery无法进行操作 onclick=(this) 接收到参数后只需要转化一下 console.log($(obj).html());

随机推荐

  1. QT5中的pro文件中为何要加入"QT += widgets"

    在pro文件里写"QT+=widgets"表示引入QtWidget这个module,qmake在生成makefile的时候,会设置好include path 和 lib path, ...

  2. Normalize.css 介绍与源码解读

    开始 Normalize.css 是一个可定制的 CSS 文件,使浏览器呈现的所有元素,更一致和符合现代标准;是在现代浏览器环境下对于CSS reset的替代. 它正是针对只需要统一的元素样式.该项目 ...

  3. MySQL Workbench是一款专为MySQL设计的ER/数据库建模工具

      MySQL  Workbench是一款专为MySQL设计的ER/数据库建模工具.它是著名的数据库设计工具DBDesigner4的继任者.你可以用MySQL  Workbench设计和创建新的数据库 ...

  4. bootstrap简章

    系统整理一遍bootstrap 的东西 1/  设置页面为H5文档类型 <!DOCTYPE html> <html lang="zh-CN"> ... &l ...

  5. 在vim保存时获得sudo权限

    在维护线上服务的时候,经常要编辑一些不属于操作用户的文件,比如只有r权限的文件,每次保存都会提示read only.这时可以使用如下命令代替原有的 :wq 命令 :w !sudo tee % 命令:w ...

  6. WPF Bug清单之(13)——应该出现却没有出现的ListView水平滚动条

    转载地址:http://www.cnblogs.com/nankezhishi/archive/2010/03/17/wpfbug13.html 我们知道ListView在内容超出控件本身范围时,默认 ...

  7. [C#]6.0新特性浅谈

    原文:[C#]6.0新特性浅谈 C#6.0出来也有很长一段时间了,虽然新的特性和语法趋于稳定,但是对于大多数程序猿来说,想在工作中用上C#6.0估计还得等上不短的一段时间.所以现在再来聊一聊新版本带来 ...

  8. 关于Char* ,CString ,WCHAR*之间的转换问题

    GDI+所有类的接口函数如果要传递字符串作为参数的话,似乎都用UNICODE串,即WCHAR*.我开始也被整得晕头转向,因为窗口编程所用往往是CString,用IO流读文件数据又得到char *.得益 ...

  9. mysql入库中文乱码问题

    mysql> show variables like '%char%';+--------------------------+----------------------------+| Va ...

  10. Oracle 中单引号和双引号的区别

    问题产生原因: insert into t_Cluster_Showresult(Outhostname,Domainlist,Iplist,Classify) values ("20145 ...