使用环境:
需要跳转界面传递数据
使用方法:
intent传递类:
类序列化:(遇到class 就 implements Serializable)
public class RectifyInfo implements Serializable{
}
开始传递:
Intent intent = new Intent(getActivity(),RectificationIssuedActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("rect",mLists.get(position));//传入类的对象
intent.putExtras(bundle);
getActivity().startActivity(intent);
取出数据:
rr = (RectifyInfo.ResultBean) getIntent().getSerializableExtra("rect");
让bean类集成parcelable(用法请百度)
public class QualityInfo implements Parcelable{
public QualityInfo(){
}
private String inspectionAbbreviation;//private String 组砌方法private String ,
private String inspectionComments;//private String private String ,
private String inspectionGrade;//private String 1private String ,
private String inspectionId;//private String c529c9acb65e43b2a232a5cde3892f14private String ,
private String inspectionStandardAb;//private String 第5.3.1条private String ,
private String inspectionStandardall;//private String private String ,
private String inspectionType;//private String 一般项目private String ,
private String inspectionValid;//private String 0private String ,
private String itemId;//private String bfcb91b91a294e718a234f5241f69663private String
//取出
protected QualityInfo(Parcel in) {
inspectionAbbreviation = in.readString();
inspectionComments = in.readString();
inspectionGrade = in.readString();
inspectionId = in.readString();
inspectionStandardAb = in.readString();
inspectionStandardall = in.readString();
inspectionType = in.readString();
inspectionValid = in.readString();
itemId = in.readString();
}
public static final Creator CREATOR = new Creator() {
@Override
public QualityInfo createFromParcel(Parcel in) {
return new QualityInfo(in);
}
@Override
public QualityInfo[] newArray(int size) {
return new QualityInfo[size];
}
};
public String getInspectionAbbreviation() {
return inspectionAbbreviation;
}
public void setInspectionAbbreviation(String inspectionAbbreviation) {
this.inspectionAbbreviation = inspectionAbbreviation;
}
public String getInspectionComments() {
return inspectionComments;
}
public void setInspectionComments(String inspectionComments) {
this.inspectionComments = inspectionComments;
}
public String getInspectionGrade() {
return inspectionGrade;
}
public void setInspectionGrade(String inspectionGrade) {
this.inspectionGrade = inspectionGrade;
}
public String getInspectionId() {
return inspectionId;
}
public void setInspectionId(String inspectionId) {
this.inspectionId = inspectionId;
}
public String getInspectionStandardAb() {
return inspectionStandardAb;
}
public void setInspectionStandardAb(String inspectionStandardAb) {
this.inspectionStandardAb = inspectionStandardAb;
}
public String getInspectionStandardall() {
return inspectionStandardall;
}
public void setInspectionStandardall(String inspectionStandardall) {
this.inspectionStandardall = inspectionStandardall;
}
public String getInspectionType() {
return inspectionType;
}
public void setInspectionType(String inspectionType) {
this.inspectionType = inspectionType;
}
public String getInspectionValid() {
return inspectionValid;
}
public void setInspectionValid(String inspectionValid) {
this.inspectionValid = inspectionValid;
}
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
//这个方法不需要修改
@Override
public int describeContents() {
return 0;
}
//写入
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(inspectionAbbreviation);
parcel.writeString(inspectionComments);
parcel.writeString(inspectionGrade);
parcel.writeString(inspectionId);
parcel.writeString(inspectionStandardAb);
parcel.writeString(inspectionStandardall);
parcel.writeString(inspectionType);
parcel.writeString(inspectionValid);
parcel.writeString(itemId);
}
开始传递:
(这一种需要做的代码比implements Ser………做的工作稍微多了一点点,但是方法更显得独一无二)
ParInfo parInfo = new ParInfo(name, website, weibo);
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putParcelable("parinfo", parInfo);// 这里调用了writeToParcel方法,向dest写数据
intent.setClass(MainActivity.this, ResultActivity.class);
intent.putExtras(bundle);
startActivity(intent);
取出数据:
ParInfo parInfo = (ParInfo) getIntent().getParcelableExtra("parinfo");
//这里调用了UsercreateFromParcel方法,返回
**********************************************************************************************************
ArrayList listObj = new ArrayList();
SerInfo serInfo1 = new SerInfo(name, website, weibo);
SerInfo serInfo2 = new SerInfo(name, website, weibo);
listObj.add(serInfo1);
listObj.add(serInfo2);
Intent intent = new Intent();
intent.setClass(MainActivity.this, ResultActivity.class);
intent.putExtra("listobj", (Serializable) listObj);
startActivity(intent);
取值的方法:
List serInfos = (List) getIntent().getSerializableExtra("listobj");
***********************************************************************************************************************************
传递基本类型数据:
Fragment fragment1 = new RectifySecondFragment();
Bundle bundle1 = new Bundle();
bundle1.putString(BUNDLE_STR,"待下发");
fragment1.setArguments(bundle1);
取出方法:
String argument = getArguments().getString(BUNDLE_STR);
Fragment fragment = new RectifyFragment();
Bundle b1 = new Bundle();
if (0 == position) {
LogUtil.e("activity传值:"+ rr.size() + "条");
b1.putSerializable(INTENTN_MODEL, (Serializable) list0);
} else if (1 == position) {
LogUtil.e("activity传值:"+ rr.size() + "条");
b1.putSerializable(INTENTN_MODEL, (Serializable) list1);
}
b1.putInt(INTENTN_ID, position);
fragment.setArguments(b1);
取出:
mLists = (List) getArguments().getSerializable(INTENTN_MODEL);
总结:
1 bean类序列化以后,可以存类和列表。
2 通过intent存取和通过arguments存取方法差不多。