Android Activity传递自定义对象

Android Activity传递自定义对象

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

A Activity

调用,PoiResultActivity

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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);
?
1
2
3
4
5
6
7
8
9
10
11
12
13
@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接受

?
1
2
3
4
5
6
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

?
1
2
3
4
5
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继承了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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多的参数,可以用下面这种方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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;
         }
     };
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值