我有集体关税,需要将其打包。
public class Tariff implements Parcelable{
private String operator;
private Discounts discount;
private boolean unlimited;
private Billings billing;
private String name;
private double price;
private double calculated;
private Call call;
private Sms sms;
我在这里找到了一些建议,但不确定我是否正确。1)对于可包裹的枚举,我发现了这一点。那能分解我枚举的所有值吗?还是我应该怎么做?
try {
type = Discounts.valueOf(in.readString());
} catch (IllegalArgumentException x) {
type = null;
}
2)对于可包裹的另一个对象(例如呼叫),我发现:如果CategoryDate类是您的一个,则也可以使其成为可包裹的。然后,在您的类的writeToParcel()调用中,可以调用this.date.writeToParcel()并将其传递给同一Parcel对象。这将导致CategoryDate类将其数据写入由CloseItPending使用的同一Parcel对象。但是我不确定我做对了。我应该怎么做?
可以打包的实际上是很多样板文件,通过手动编码,很有可能会出错。
使用此网站:http : //www.parcelabler.com/
该工具会自动为您的班级生成可拆分字段。切记也要使声明为类变量的类也可拆分。
结果将是这样的:
public class Tariff implements Parcelable {
private String operator;
private Discounts discount;
private boolean unlimited;
private Billings billing;
private String name;
private double price;
private double calculated;
private Call call;
private Sms sms;
protected Tariff(Parcel in) {
operator = in.readString();
discount = (Discounts) in.readValue(Discounts.class.getClassLoader());
unlimited = in.readByte() != 0x00;
billing = (Billings) in.readValue(Billings.class.getClassLoader());
name = in.readString();
price = in.readDouble();
calculated = in.readDouble();
call = (Call) in.readValue(Call.class.getClassLoader());
sms = (Sms) in.readValue(Sms.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(operator);
dest.writeValue(discount);
dest.writeByte((byte) (unlimited ? 0x01 : 0x00));
dest.writeValue(billing);
dest.writeString(name);
dest.writeDouble(price);
dest.writeDouble(calculated);
dest.writeValue(call);
dest.writeValue(sms);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Tariff> CREATOR = new Parcelable.Creator<Tariff>() {
@Override
public Tariff createFromParcel(Parcel in) {
return new Tariff(in);
}
@Override
public Tariff[] newArray(int size) {
return new Tariff[size];
}
};
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句