• 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;
    		}
    	};
    
    }
    

      

  • 相关阅读:
    实现免费WiFi无线共享
    详解spring配置文件
    P1886 滑动窗口 /【模板】单调队列
    P3370 【模板】字符串哈希
    P3371 【模板】单源最短路径(弱化版)
    P3367 【模板】并查集
    P1177 【模板】快速排序
    P3382 【模板】三分法
    P3374 【模板】树状数组 1
    P1226 【模板】快速幂||取余运算
  • 原文地址:https://www.cnblogs.com/warrior/p/2469752.html
Copyright © 2020-2023  润新知