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. hdu 4709 Herding hdu 2013 热身赛

    题意:给出笛卡尔坐标系上 n 个点,n不大于100,求出这些点中能围出的最小面积. 可以肯定的是三个点围成的面积是最小的,然后就暴力枚举,计算任意三点围成的面积.刚开始是求出三边的长,然后求面积,运算 ...

  2. PHP 魔术常量__FUNCTION__与__METHOD__的区别

    __FUNCTION__ 返回 函数名称(PHP 4.3.0 新加).自 PHP 5 起本常量返回该函数被定义时的名字(区分大小写).在 PHP 4 中该值总是小写字母的.    __METHOD__ ...

  3. c语言 文件写入和读取

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define N 10 struct studen ...

  4. 14-利用SVD简化数据

    参考:http://blog.csdn.net/geekmanong/article/details/50494936 http://www.2cto.com/kf/201503/383087.htm ...

  5. mongo设计(二)

    原文:http://blog.mongodb.org/post/87892923503/6-rules-of-thumb-for-mongodb-schema-design-part-2 By Wil ...

  6. jQuery创建节点

    注:摘自<锋利的jQuery(第二版)> 创建元素节点 例如要创建两个<li>元素节点,并且要把它们作为<ul>元素节点的子节点添加到DOM节点树上.完成这个任务需 ...

  7. OpenStreetMap(OSM) JMap Viewer(Java swing map)

    This article from:http://wiki.openstreetmap.org/wiki/JMapViewer JMapViewer is a java component which ...

  8. mit java open course assignment #4

    package come; public class Library { // Add the missing implementation to this class String realLoca ...

  9. Spring流程

    Spring Web Flow是Spring框架的子项目,作用是让程序按规定流程运行. 1 安装配置Spring Web Flow 虽然Spring Web Flow是Spring框架的子项目,但它并 ...

  10. [LeetCode] Rotate Image [26]

    题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...