Parcelableオブジェクト配列をActivity間で渡す

2080 ワード

1.まずあなたの相手はParcelableに
Person.java
public class Person implements Parcelable
{
	private String name;
	private String address;
	
	public Person(String name, String address)
	{
		this.name = name;
		this.address = address;
	}
	
	public String getName()
	{
		return this.name;
	}
	
	public String getAddress()
	{
		return this.address;
	}
	
	/**	 */
	public int describeContents() 
	{
		return 0;
	}
	
	public void writeToParcel(Parcel dest, int flags)
	{
		dest.writeString(name);
		dest.writeString(address);
	}
	
	public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>()
	{
		public Person createFromParcel(Parcel source)
		{
			Person person = new Person("","");
			person.name = source.readString();
			person.address = source.readString();
			return person;
		}
		public Person[] newArray(int size)
		{
			return new Person[size];
		}
	};

}

 
2.
トランスファ
class BtnSeeLocationListener implements OnClickListener
    {

		public void onClick(View v)   
		{
			
			Intent intent = new Intent();
			intent.setClass(Main.this, MapViewActivity.class);
			Bundle bundle = new Bundle();

			bundle.putParcelableArrayList(SER_KEY, personList);

                        // Bundle , 
                       //intent.putParcelableArrayListExtra(SER_KEY, personList);

                        intent.putExtras(bundle);
			startActivity(intent);
		}
    	
    }

 
3.受入
 
Intent intent = getIntent();
personList = (ArrayList<Person>)intent.getExtras().getSerializable(Main.SER_KEY);//