版权声明:本文为博主原创文章,未经博主允许不得转载。
方式一:通过Intent传递实体的方法,直接进行传递
来源页:
Intent intent = new Intent(MyCarListActivity.this, ModifyMyCarTypeActivity.class);
intent.putExtra(IntentPostConstants.MyCarListEntity, myCarListEntity);
startActivity(intent);
目标页:
/获取数据
if(getIntent()!=null){
MyCarListEntity myCarListEntity= (MyCarListEntity) getIntent()
.getSerializableExtra(IntentPostConstants.MyCarListEntity)
if (myCarListEntity != null) {
carBrand.setText(myCarListEntity.getTypeName())
maycar_type_carnumber.setText(myCarListEntity.getPlateNumber())
ID = myCarListEntity.getID()
}
}
方式二:使用Bundle传递序列化对象
来源页:
//传递数据,进行跳转
Intent intent = new Intent()
Bundle bundle = new Bundle()
bundle.putSerializable(IntentPostConstants.MyCarListEntity, myCarListEntity)
intent.putExtras(bundle)
intent.setClass(MyCarListActivity.this, ModifyMyCarTypeActivity.class)
startActivity(intent)
目标页:
//获取intent值
if (getIntent() != null) {
Bundle bundle = getIntent().getExtras()
if (bundle != null) {
MyCarListEntity myCarListEntity = (MyCarListEntity)
bundle.getSerializable(IntentPostConstants.MyCarListEntity)
if (myCarListEntity != null) {
carBrand.setText(myCarListEntity.getTypeName())
maycar_type_carnumber.setText(myCarListEntity.getPlateNumber())
ID = myCarListEntity.getID()
}
}
}
声明intent传递常量:
/**
* Created by zhang on 2016/7/31.
* <p>
* intent数据传递常量类
*/
public class IntentPostConstants {
public final static String MyCarListEntity = "MyCarListEntity";
}
对象需要序列化:
public class MyCarListEntity implements Serializable {
private String ID;
private String BuyTime;
private String CarModelId;
……………………
}
startActivityForResult的数据传递:
来源页:
Intent intent = new Intent(MyCarListActivity.this, ModifyMyCarTypeActivity.class);
intent.putExtra(IntentPostConstants.MyCarListEntity, myCarListEntity);
startActivityForResult(intent, 8);
执行的回调:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 8:
if (resultCode == 6 && data != null) {
CarMakeListEntity carMakeListEntity = (CarMakeListEntity)
data.getSerializableExtra(IntentPostConstants.CarMakeListEntity);
carBrand.setText(carMakeListEntity.getTypeName());
}
}
}
目标页:
CarMakeListEntity entity = (CarMakeListEntity) data;
Intent intent = new Intent();
intent.putExtra(IntentPostConstants.CarMakeListEntity, entity);
setResult(6, intent);
finish();