Android Activity傳遞自定義對象


http://developer.android.com/reference/android/os/Parcelable.html

A Activity

調用,PoiResultActivity

Intent intent = new Intent();
intent.setClass(this, PoiResultActivity.class);
Bundle bundle = new Bundle();

ArrayList<PoiInfoParcelable> allPoiInfo = new ArrayList<PoiInfoParcelable>();
ArrayList<MKPoiInfo> allPoi = result.getAllPoi();
for (MKPoiInfo poiInfo : allPoi) {
	allPoiInfo.add(new PoiInfoParcelable(poiInfo));
}
bundle.putParcelableArrayList(MAP_ALL_POI_RESULT, allPoiInfo);
intent.putExtras(bundle);

startActivityForResult(intent, CODE_POI_RESULT);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

	if (resultCode == CODE_POI_INFO_PARCELABLE) {
		PoiInfoParcelable poiInfo = (PoiInfoParcelable) data.getExtras().get(MAP_SELECTED_POI_INFO);
		int position = data.getExtras().getInt(MAP_SELECTED_POI_INFO_POSITION);
		moveToMyLocation(poiInfo.pt);

		mPoioverlay.setShowPopView(position);
	}

	super.onActivityResult(requestCode, resultCode, data);
}

PoiResultActivity接受

private void bindData() {
	Bundle bundle = getIntent().getExtras();
	ArrayList<PoiInfoParcelable> allPoiResult =  bundle.getParcelableArrayList(MAP_ALL_POI_RESULT);
	PoiResultItemAdapter adapter = new PoiResultItemAdapter(this, allPoiResult);
	mListView.setAdapter(adapter);
}

PoiResultActivity返回到A Activity

Intent intent = new Intent();
intent.putExtra(MAP_SELECTED_POI_INFO_POSITION, position);
intent.putExtra(MAP_SELECTED_POI_INFO, (PoiInfoParcelable)obj);
setResult(CODE_POI_INFO_PARCELABLE, intent);
finish();

這個地方應為我要傳遞一個百度地圖的地理信息去下個Activity中,所以直接從MKPoiInfo繼承了

public class PoiInfoParcelable extends MKPoiInfo implements Parcelable {

	public PoiInfoParcelable(){
		
	}

	
	public PoiInfoParcelable(MKPoiInfo poiInfo) {
		this.address = poiInfo.address;
		this.city = poiInfo.city;
		this.ePoiType = poiInfo.ePoiType;
		this.name = poiInfo.name;
		this.phoneNum = poiInfo.phoneNum;
		this.postCode = poiInfo.postCode;
		this.pt = poiInfo.pt;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(this.address);
		dest.writeString(this.city);
		dest.writeString(this.name);
		dest.writeString(this.phoneNum);
		dest.writeString(this.postCode);
		dest.writeInt(this.ePoiType);
		dest.writeInt(this.pt.getLatitudeE6());
		dest.writeInt(this.pt.getLongitudeE6());
	}
	
	public final static Parcelable.Creator<PoiInfoParcelable> CREATOR = new Creator<PoiInfoParcelable>() {
		
		@Override
		public PoiInfoParcelable[] newArray(int size) {
			return new PoiInfoParcelable[size];
		}
		
		@Override
		public PoiInfoParcelable createFromParcel(Parcel source) {
			PoiInfoParcelable poiInfo = new PoiInfoParcelable();
			poiInfo.address = source.readString();
			poiInfo.city = source.readString();
			poiInfo.name = source.readString();
			poiInfo.phoneNum = source.readString();
			poiInfo.postCode = source.readString();
			poiInfo.ePoiType = source.readInt();
			poiInfo.pt = new GeoPoint(source.readInt(), source.readInt());
			return poiInfo;
		}
	};
}

如果是復雜的情況,就是N多的參數,可以用下面這種方式

public class PoiResultParcelable implements Parcelable {

	private ArrayList<PoiInfoParcelable> poiInfoList;

	public ArrayList<PoiInfoParcelable> getPoiInfoList() {
		return poiInfoList;
	}

	public void setPoiInfoList(ArrayList<PoiInfoParcelable> poiInfoList) {
		this.poiInfoList = poiInfoList;
	}

	public PoiResultParcelable(Parcel in) {
		readFromParcel(in);
	}

	private void readFromParcel(Parcel in) {
		in.readTypedList(poiInfoList, PoiInfoParcelable.CREATOR);
	}

	@Override
	public int describeContents() {
		return poiInfoList == null ? 0 : poiInfoList.size();
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeList(poiInfoList);
	}

	public final static Parcelable.Creator<PoiResultParcelable> CREATOR = new Creator<PoiResultParcelable>() {

		@Override
		public PoiResultParcelable[] newArray(int size) {
			return new PoiResultParcelable[size];
		}

		@Override
		public PoiResultParcelable createFromParcel(Parcel source) {
			PoiResultParcelable poiResult = new PoiResultParcelable(source);
			return poiResult;
		}
	};

}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM