常用链接

统计

最新评论

Activitie之间传对象,通过Parcelable(转)

对象必须实现Serializable,对象代码如下:

Java代码 
  1. import java.io.Serializable;  
  2.   
  3. import android.graphics.drawable.Drawable;  
  4.   
  5. /** 
  6.  * 包括软件信息及评论 
  7.  * */  
  8. public class MyApplicationInfo extends Object implements Serializable{  
  9.       
  10.     /** 
  11.      *  
  12.      */  
  13.     private static final long serialVersionUID = -5151302508106116740L;  
  14.   
  15.     //程序名称  
  16.     private String title;  
  17.       
  18.     //程序名称  
  19.     private String label;  
  20.       
  21.     //程序版本  
  22.     private String version;  
  23.       
  24.     //新版本  
  25.     private String newVersion;  
  26.       
  27.     //内容介绍  
  28.     private String content;  
  29.       
  30.     //作者  
  31.     private String author;  
  32.       
  33.     //图标链接  
  34.     private String icon;  
  35.       
  36.     //图标  
  37.     private Drawable dicon;  
  38.       
  39.     //演示图片动态数组  
  40.     private String[] Demos;  
  41.       
  42.     //价格  
  43.     private String price;  
  44.       
  45.     //总共5星,以数字形式存储  
  46.     private String star;  
  47.       
  48.     //链接地址  
  49.     private String detailaddress;  
  50.       
  51.     //评论包括:评论者、评论时间、评论内容、评论星级  
  52.     private Details details;  
  53.       
  54.     //包名  
  55.     private String packageName;  
  56.       
  57.     //启动程序Class  
  58.     private String className;  
  59.       
  60.     public String getTitle() {  
  61.           
  62.         return title;  
  63.     }  
  64.   
  65.     public void setTitle(String title) {  
  66.           
  67.         this.title = title;  
  68.     }  
  69.   
  70.     public String getVersion() {  
  71.           
  72.         return version;  
  73.     }  
  74.   
  75.     public void setVersion(String version) {  
  76.           
  77.         this.version = version;  
  78.     }  
  79.   
  80.     public String getContent() {  
  81.           
  82.         return content;  
  83.     }  
  84.   
  85.     public void setContent(String content) {  
  86.           
  87.         this.content = content;  
  88.     }  
  89.   
  90.     public String getAuthor() {  
  91.           
  92.         return author;  
  93.     }  
  94.   
  95.     public void setAuthor(String author) {  
  96.           
  97.         this.author = author;  
  98.     }  
  99.   
  100.     public String getIcon() {  
  101.           
  102.         return icon;  
  103.     }  
  104.   
  105.     public void setIcon(String icon) {  
  106.           
  107.         this.icon = icon;  
  108.     }  
  109.   
  110.     public String getPrice() {  
  111.           
  112.         return price;  
  113.     }  
  114.   
  115.     public void setPrice(String price) {  
  116.           
  117.         this.price = price;  
  118.     }  
  119.   
  120.     public String getStar() {  
  121.           
  122.         return star;  
  123.     }  
  124.   
  125.     public void setStar(String star) {  
  126.           
  127.         this.star = star;  
  128.     }  
  129.   
  130.   
  131.     public String getDetailaddress() {  
  132.           
  133.         return detailaddress;  
  134.     }  
  135.   
  136.     public void setDetailaddress(String detailaddress) {  
  137.           
  138.         this.detailaddress = detailaddress;  
  139.     }  
  140.   
  141.     public String[] getDemos() {  
  142.           
  143.         return Demos;  
  144.     }  
  145.   
  146.     public void setDemos(String[] demos) {  
  147.           
  148.         Demos = demos;  
  149.     }  
  150.       
  151.   
  152.     public Details getDetails() {  
  153.           
  154.         return details;  
  155.     }  
  156.   
  157.     public void setDetails(Details details) {  
  158.           
  159.         this.details = details;  
  160.     }  
  161.   
  162.     public String getPackageName() {  
  163.           
  164.         return packageName;  
  165.     }  
  166.   
  167.     public void setPackageName(String packageName) {  
  168.           
  169.         this.packageName = packageName;  
  170.     }  
  171.   
  172.     public Drawable getDicon() {  
  173.           
  174.         return dicon;  
  175.     }  
  176.   
  177.     public void setDicon(Drawable dicon) {  
  178.           
  179.         this.dicon = dicon;  
  180.     }  
  181.   
  182.     public String getLabel() {  
  183.           
  184.         return label;  
  185.     }  
  186.   
  187.     public void setLabel(String label) {  
  188.           
  189.         this.label = label;  
  190.     }  
  191.   
  192.     public String getNewVersion() {  
  193.           
  194.         return newVersion;  
  195.     }  
  196.   
  197.     public void setNewVersion(String newVersion) {  
  198.           
  199.         this.newVersion = newVersion;  
  200.     }  
  201.   
  202.     public String getClassName() {  
  203.           
  204.         return className;  
  205.     }  
  206.   
  207.     public void setClassName(String className) {  
  208.           
  209.         this.className = className;  
  210.     }  
  211.   
  212.       
  213.   
  214.       
  215. }  

 

自定义:AppParcelable

Java代码 
  1. import android.os.Parcel;  
  2. import android.os.Parcelable;  
  3.   
  4. import com.tcad.marketassistant.vo.MyApplicationInfo;  
  5.   
  6. public class AppParcelable implements Parcelable {  
  7.   
  8.     private MyApplicationInfo info;  
  9.       
  10.     public AppParcelable(Parcel source){  
  11.           
  12.         info = (MyApplicationInfo)source.readValue(MyApplicationInfo.class.getClassLoader());  
  13.               
  14.     }  
  15.       
  16.     public AppParcelable(MyApplicationInfo info){  
  17.           
  18.         this.info = info;  
  19.     }  
  20.       
  21.     public int describeContents() {  
  22.           
  23.         return 0;  
  24.     }  
  25.   
  26.     public void writeToParcel(Parcel dest, int flags) {  
  27.           
  28.         dest.writeValue(info);  
  29.     }  
  30.   
  31.   
  32.     public static final Parcelable.Creator<AppParcelable> CREATOR = new Parcelable.Creator<AppParcelable>() {  
  33.   
  34.         public AppParcelable createFromParcel(Parcel source) {  
  35.               
  36.             return new AppParcelable(source);  
  37.         }  
  38.   
  39.         public AppParcelable[] newArray(int size) {  
  40.               
  41.         //  return new AppParcelable[size];  
  42.             throw new UnsupportedOperationException();   
  43.         }  
  44.           
  45.     };  
  46.       
  47.     public MyApplicationInfo getInfo(){  
  48.           
  49.         return info;  
  50.     }  
  51. }  

 调用代码,发送:

Java代码 
  1. AppParcelable parcelable = new AppParcelable(info);  
  2. //Info为MyApplicationInfo对象            
  3.             // 发送对象  
  4.             intent.putExtra("app_parcelable", parcelable);  
  5.               
  6.             startActivity(intent);  

 接收:

Java代码 
  1. AppParcelable p = getIntent().getParcelableExtra("app_parcelable");  
  2.               
  3.             MyApplicationInfo info = p.getInfo();  

 

posted on 2010-09-10 16:00 九宝 阅读(2126) 评论(0)  编辑  收藏 所属分类: android


只有注册用户登录后才能发表评论。


网站导航: