Activity传递一个或者多个对象

Activity之间传递对象,或者通过Bundle传递对象的两种方式。

1:Serializable方式
  传递一个对象

2:Parcelable方式
  传递一个对象、传递多个对象(ArrayList<Object>)


方式一:Serializable

     传递类:
    
public class CustomeClass implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7060210544600464481L;
	private String name;
	private String id;
	private int age;
	private String sex;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}

}



       发送部分:
      
        CustomeClass cc = new CustomeClass();
        cc.setAge(21);
        cc.setId("123456");
        cc.setName("mingkg21");
        cc.setSex("男");
        
        Intent intent = new Intent(this, PersonInfo.class);
        intent.putExtra("PERSON_INFO", cc);
        startActivity(intent);


       
       接收部分:
      
        Intent intent = getIntent();
	CustomeClass cc = CustomeClass)intent.getSerializableExtra("PERSON_INFO");
	setTextView(R.id.id, cc.getId());
	setTextView(R.id.name, cc.getName());
	setTextView(R.id.sex, cc.getSex());
	setTextView(R.id.age, String.valueOf(cc.getAge()));



方式二:Parcelable

     传递类:
    
public class CustomeParcelable implements Parcelable {
	
	private String name;
	private String id;
	private int age;
	private String sex;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public static final Parcelable.Creator<CustomeParcelable> CREATOR = new Creator<CustomeParcelable>(){

		public CustomeParcelable createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			CustomeParcelable cus = new CustomeParcelable();
			cus.name = source.readString();
			cus.id = source.readString();
			cus.age = source.readInt();
			cus.sex = source.readString();
			return cus;
		}

		public CustomeParcelable[] newArray(int size) {
			// TODO Auto-generated method stub
			return new CustomeParcelable[size];
		}
		
	};

	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeString(name);
		dest.writeString(id);
		dest.writeInt(age);
		dest.writeString(sex);
	}

}


       发送部分:
      
CustomeParcelable cc = new CustomeParcelable();
cc.setAge(21);
cc.setId("123456");
cc.setName("mingkg21");
cc.setSex("男");
        
Intent intent = new Intent(this, PersonInfo.class);
intent.putExtra("PERSON_INFO", cc);
startActivity(intent);


        接受部分:
       
Intent intent = getIntent();
CustomeParcelable cc = intent.getParcelableExtra("PERSON_INFO");
setTextView(R.id.id, cc.getId());
setTextView(R.id.name, cc.getName());
setTextView(R.id.sex, cc.getSex());
setTextView(R.id.age, String.valueOf(cc.getAge()));



以上为Parcelable传递一个对象,若要实现传递多个对象,
传递部分:
Bundle bundle = new Bundle();
		bundle.putParcelableArrayList("mP3TagForNetDTOs",mP3TagForNetDTOs);
		msg.setData(bundle);
		endDocNotice.sendMessage(msg);


接受部分:
Bundle bundle =  msg.getData();
				mP3TagForNetDTOs = bundle.getParcelableArrayList("mP3TagForNetDTOs");


此文转载,来自: http://mingkg21.iteye.com/blog/438913
                                                          
                                                              Thanks.

你可能感兴趣的:(Activity)