`
zengyan2012
  • 浏览: 409830 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论
阅读更多

转自:http://aijiawang-126-com.iteye.com/blog/643762

 

可以查看这两篇文章: 
1,通过Serializable传递一个类对象的例子 
http://mingkg21.iteye.com/blog/438913 
2,通过Parcelable传递一个类对象的例子 
http://mingkg21.iteye.com/blog/463895 

而若需要传递多个类对象的时候就必须用Parcelable来封装类,然后将其存放在ArrayList, 
我从网上下载的一个例子该成这种情况的:可以看其代码: 

对象:

 

Java代码   收藏代码
  1. package cn.wizhy;  
  2.   
  3. import android.os.Parcel;  
  4. import android.os.Parcelable;  
  5.   
  6. public class Phone implements Parcelable{  
  7.     String type;  
  8.     String company;  
  9.     int price;  
  10.     public Phone(String t,String c,int p) {  
  11.         type=t;  
  12.         company=c;  
  13.         price=p;  
  14.     }  
  15.     public Phone() {  
  16.         // TODO Auto-generated constructor stub  
  17.     }  
  18.     public String getType() {  
  19.         return type;  
  20.     }  
  21.     public void setType(String type) {  
  22.         this.type = type;  
  23.     }  
  24.     public String getCompany() {  
  25.         return company;  
  26.     }  
  27.     public void setCompany(String company) {  
  28.         this.company = company;  
  29.     }  
  30.     public int getPrice() {  
  31.         return price;  
  32.     }  
  33.     public void setPrice(int price) {  
  34.         this.price = price;  
  35.     }  
  36.     public static final Parcelable.Creator<Phone> CREATOR = new Creator<Phone>(){  
  37.   
  38.         @Override  
  39.         public Phone createFromParcel(Parcel source) {  
  40.             // TODO Auto-generated method stub  
  41.             Phone cus = new Phone();    
  42.                 cus.type = source.readString();    
  43.                 cus.company = source.readString();    
  44.                 cus.price = source.readInt();    
  45.                 return cus;    
  46.         }  
  47.   
  48.         @Override  
  49.         public Phone[] newArray(int size) {  
  50.             // TODO Auto-generated method stub  
  51.             return new Phone[size];  
  52.         }    
  53.           
  54.     };  
  55.     @Override  
  56.     public int describeContents() {  
  57.         // TODO Auto-generated method stub  
  58.         return 0;  
  59.     }  
  60.     @Override  
  61.     public void writeToParcel(Parcel dest, int flags) {  
  62.         // TODO Auto-generated method stub  
  63.         dest.writeString(type);  
  64.         dest.writeString(company);  
  65.         dest.writeInt(price);  
  66.           
  67.     }  
  68. }  



第一个Activity,构造类将其存放到Arraylist里,并通过Intent传给第二个Activity 

Java代码   收藏代码
  1. package cn.wizhy;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8.   
  9. public class Demo extends Activity {  
  10.     ArrayList<Phone> info = new ArrayList<Phone>();  
  11.     public Phone phone;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         insertPhone();  
  17.         Intent intent = new Intent(this,Demo2.class);  
  18. //        Bundle bundle = new Bundle();  
  19. //        bundle.putSerializable("phone", phone);  
  20.          
  21. //        intent.putExtras(bundle);  
  22.         phone = new Phone("goole","G1",6000);  
  23.         info.add(phone);  
  24.         phone = new Phone("apple""iphone3G"5000);  
  25.         info.add(phone);  
  26.        intent.putExtra("phones", info);  
  27.           
  28.         startActivity(intent);  
  29.     }  
  30.     public void insertPhone(){  
  31.         phone= new Phone("apple""iphone3G"5000);  
  32.     }  
  33.       
  34. }  



第二个Activity接受数据: 

Java代码   收藏代码
  1. package cn.wizhy;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8.   
  9. public class Demo2 extends Activity {  
  10.     ArrayList<Phone> info = new ArrayList<Phone>();  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         Intent intent = getIntent();  
  15.         info =intent.getParcelableArrayListExtra("phones");  
  16.         for(int i=0;i<info.size();i++){  
  17.             System.out.println("type="+info.get(i).type+"   company="+info.get(i).company+" price"+info.get(i).price);  
  18.         }  
  19.     }  
  20. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics