zoukankan      html  css  js  c++  java
  • 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;
    		}
    	};
    
    }
    

      

  • 相关阅读:
    Linux安全加固
    mosquitto
    Docker设置2375端口
    linux shell 常见的时间戳操作
    tar打包并且排除log目录
    spring boot2整合shiro安全框架实现前后端分离的JWT token登录验证
    http post
    scp命令 Linux和Windows文件互传
    git did not exit cleanly (exit code 1) 的解决办法
    windows安装TortoiseGit详细使用教程【基础篇】
  • 原文地址:https://www.cnblogs.com/warrior/p/2469752.html
Copyright © 2011-2022 走看看