可以查看这两篇文章:
1,通过Serializable传递一个类对象的例子
http://mingkg21.iteye.com/blog/438913
2,通过Parcelable传递一个类对象的例子
http://mingkg21.iteye.com/blog/463895
而若需要传递多个类对象的时候就必须用Parcelable来封装类,然后将其存放在ArrayList,
我从网上下载的一个例子该成这种情况的:可以看其代码:
对象:
第一个Activity,构造类将其存放到Arraylist里,并通过Intent传给第二个Activity
第二个Activity接受数据:
1,通过Serializable传递一个类对象的例子
http://mingkg21.iteye.com/blog/438913
2,通过Parcelable传递一个类对象的例子
http://mingkg21.iteye.com/blog/463895
而若需要传递多个类对象的时候就必须用Parcelable来封装类,然后将其存放在ArrayList,
我从网上下载的一个例子该成这种情况的:可以看其代码:
对象:
- package cn.wizhy;
- import android.os.Parcel;
- import android.os.Parcelable;
- public class Phone implements Parcelable{
- String type;
- String company;
- int price;
- public Phone(String t,String c,int p) {
- type=t;
- company=c;
- price=p;
- }
- public Phone() {
- // TODO Auto-generated constructor stub
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getCompany() {
- return company;
- }
- public void setCompany(String company) {
- this.company = company;
- }
- public int getPrice() {
- return price;
- }
- public void setPrice(int price) {
- this.price = price;
- }
- public static final Parcelable.Creator<Phone> CREATOR = new Creator<Phone>(){
- @Override
- public Phone createFromParcel(Parcel source) {
- // TODO Auto-generated method stub
- Phone cus = new Phone();
- cus.type = source.readString();
- cus.company = source.readString();
- cus.price = source.readInt();
- return cus;
- }
- @Override
- public Phone[] newArray(int size) {
- // TODO Auto-generated method stub
- return new Phone[size];
- }
- };
- @Override
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- // TODO Auto-generated method stub
- dest.writeString(type);
- dest.writeString(company);
- dest.writeInt(price);
- }
- }
第一个Activity,构造类将其存放到Arraylist里,并通过Intent传给第二个Activity
- package cn.wizhy;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- public class Demo extends Activity {
- ArrayList<Phone> info = new ArrayList<Phone>();
- public Phone phone;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- insertPhone();
- Intent intent = new Intent(this,Demo2.class);
- // Bundle bundle = new Bundle();
- // bundle.putSerializable("phone", phone);
- // intent.putExtras(bundle);
- phone = new Phone("goole","G1",6000);
- info.add(phone);
- phone = new Phone("apple", "iphone3G", 5000);
- info.add(phone);
- intent.putExtra("phones", info);
- startActivity(intent);
- }
- public void insertPhone(){
- phone= new Phone("apple", "iphone3G", 5000);
- }
- }
第二个Activity接受数据:
- package cn.wizhy;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- public class Demo2 extends Activity {
- ArrayList<Phone> info = new ArrayList<Phone>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Intent intent = getIntent();
- info =intent.getParcelableArrayListExtra("phones");
- for(int i=0;i<info.size();i++){
- System.out.println("type="+info.get(i).type+" company="+info.get(i).company+" price"+info.get(i).price);
- }
- }
- }