Android(安卓)Activity传递自定义对象
16lz
2021-12-21
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();ArrayListallPoiInfo = new ArrayList ();ArrayList 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);
@Overrideprotected 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();ArrayListallPoiResult = 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;}@Overridepublic int describeContents() {return 0;}@Overridepublic 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.CreatorCREATOR = new Creator () {@Overridepublic PoiInfoParcelable[] newArray(int size) {return new PoiInfoParcelable[size];}@Overridepublic 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 ArrayListpoiInfoList;public ArrayList getPoiInfoList() {return poiInfoList;}public void setPoiInfoList(ArrayList poiInfoList) {this.poiInfoList = poiInfoList;}public PoiResultParcelable(Parcel in) {readFromParcel(in);}private void readFromParcel(Parcel in) {in.readTypedList(poiInfoList, PoiInfoParcelable.CREATOR);}@Overridepublic int describeContents() {return poiInfoList == null ? 0 : poiInfoList.size();}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeList(poiInfoList);}public final static Parcelable.Creator CREATOR = new Creator () {@Overridepublic PoiResultParcelable[] newArray(int size) {return new PoiResultParcelable[size];}@Overridepublic PoiResultParcelable createFromParcel(Parcel source) {PoiResultParcelable poiResult = new PoiResultParcelable(source);return poiResult;}};}
更多相关文章
- android 调用本地音乐播放器
- Android的一些基本常识
- android string.xml占位符
- Android(安卓)程序调用系统发信息程序
- 在android里面调用javascript代码
- Android路径大全
- android listview 调用sqlsite数据库显示数据
- Android(安卓)Fragment
- Android(安卓)Studio Run程序时自动执行自定义task