我创建了一个具有自定义类对象/变量的类,并且我希望将此类设置为parcelable以将其传递给意图,以便我在下一个活动中收到响应
更详细的
我先上课,即
public class Data implements Parcelable{
@SerializedName("barlist")
Bar bar_list[];
public Bar[] getBarLst() {
return bar_list;
}
public void setBarLst(Bar lst[]) {
this.bar_list = lst;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeParcelableArray(bar_list, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
}
public static final Parcelable.Creator<Data> CREATOR = new Creator<Data>() {
public Data createFromParcel(Parcel source) {
Data data = new Data();
data.bar_list = (Bar[]) source.readParcelableArray(this.getClass().getClassLoader());
return data;
}
@Override
public Data[] newArray(int size) {
// TODO Auto-generated method stub
return new Data[size];
}
};
}
在上面的类中,我有一个自定义类型的对象/变量,即类型为Bar
我的下一堂课是::
public class Bar implements Parcelable{
@SerializedName("name")
String Name;
@SerializedName("sex")
String sex;
@SerializedName("type")
String type;
@SerializedName("userid")
String userId;
@SerializedName("contactno")
String ContactNo;
@SerializedName("zipcode")
String zipCode;
@SerializedName("address")
String Address;
@SerializedName("email")
String Email;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getContactNo() {
return ContactNo;
}
public void setContactNo(String contactNo) {
ContactNo = contactNo;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(Name);
parcel.writeString(sex);
parcel.writeString(type);
parcel.writeString(userId);
parcel.writeString(ContactNo);
parcel.writeString(zipCode);
parcel.writeString(Address);
parcel.writeString(Email);
}
public static final Parcelable.Creator<Bar> CREATOR = new Creator<Bar>() {
public Bar createFromParcel(Parcel source) {
Bar barlst = new Bar();
barlst.Name = source.readString();
barlst.sex = source.readString();
barlst.ContactNo = source.readString();
barlst.type = source.readString();
barlst.userId = source.readString();
barlst.zipCode = source.readString();
barlst.Address = source.readString();
barlst.Email = source.readString();
return barlst;
}
@Override
public Bar[] newArray(int size) {
// TODO Auto-generated method stub
return new Bar[size];
}
};
}
我想让一个数据类(第一类)对象可以分解,所以在我的第一个活动中我做了一些这样的事情
EmptyRequest empt = new EmptyRequest();
Data responsestr = userManager.getMainMenuItems(empt,"url","Post","getBarList");
Intent myintent = new Intent(MainMenuPageActivity.this, BarListPageActivity.class);
Bundle mbundle = new Bundle();
mbundle.putParcelable("BARLIST", responsestr);
myintent.putExtras(mbundle);
startActivity(myintent);
直到这里我的代码工作正常,我将类型数据的响应变量保存到parcelable
中在我的下一个活动中,我试图获取像这样的数据对象
Data responseStr = (Response)getIntent().getParcelableExtra("BARLIST");
获取类型数据的对象,但这不起作用,并且在第二个活动中找到了第二个活动类,但我的调试器到达了第二个活动。
提前致谢....
答案 0 :(得分:0)
使用
getIntent().getExtras().getParcelableExtra("BARLIST");