2017-04-10 52 views
1

枚舉Parcelable對象在Android項目中,我有這些領域類:與Android的

public class TransactionHistoryDetail1 implements Parcelable, DatabaseEnabled{ 

    private long id; 
    private static final String TABLE_NAME = "TransactionHistoryDetail"; 
    private static final String EMPTY_STRING = ""; 

    @XmlElement(name = "TxUd", required = true) 
    private String TxUd; 
    @XmlElement(name = "TxLclDtTm", required = true) 
    private Date TxLclDtTm; 
    @XmlElement(name = "CcyCd") 
    private CurrencyCode CcyCd; 
    @XmlElement(name = "Amt") 
    private BigDecimal Amt; 
    @XmlElement(name = "PmttpCd", required = true) 
    private PaymentTypeCode PmttpCd; 
    @XmlElement(name = "OprtnCd", required = true) 
    private OperationCode OprtnCd; 
    @XmlElement(name = "AppLabltpCd", required = true) 
    private AppLabelTypeCode AppLabltpCd; 
    @XmlElement(name = "PdSrl") 
    private String PdSrl; 
    @XmlElement(name = "PdBrndDsc") 
    private String PdBrndDsc; 
    @XmlElement(name = "UsrEml") 
    private String UsrEml; 
    @XmlElement(name = "CstmrAdr") 
    private String CstmrAdr; 
    @XmlElement(name = "TxDtlDsc") 
    private String TxDtlDsc; 
    @XmlElement(name = "TxRltdInd") 
    private boolean TxRltdInd; 
    @XmlElement(name = "TxSttsCd", required = true) 
    private TransactionStatusCode TxSttsCd; 
    @XmlElement(name = "UpdtDt", required = true) 
    private Date UpdtDt; 
... 
} 

我試着去寫和讀這個類的對象作爲Parcelables並但林不知道如何寫和讀枚舉。

我writeToParcel方法是這樣的:

@Override 
public void writeToParcel(Parcel dest, int flags) { 

    SimpleDateFormat sdf = new SimpleDateFormat(JsonBuilder.DateFormat); 

    dest.writeLong(id); 
    dest.writeDouble(Amt.doubleValue()); 
    dest.writeString(PdSrl); 
    dest.writeString(PdBrndDsc); 
    dest.writeString(TxUd); 
    dest.writeString(PdSrl); 
    dest.writeString(UsrEml); 
    dest.writeString(CstmrAdr); 
    dest.writeString(TxDtlDsc); 
    dest.writeString((CcyCd == null) ? "" : CcyCd.name()); 
    dest.writeString((PmttpCd == null) ? "" : PmttpCd.name()); 
    dest.writeString((OprtnCd == null) ? "" : OprtnCd.name()); 
    dest.writeString((AppLabltpCd == null) ? "" : AppLabltpCd.name()); 
    dest.writeString(sdf.format(TxLclDtTm)); 
    dest.writeString(sdf.format(UpdtDt)); 
    dest.writeByte((byte) (TxRltdInd ? 1 : 0)); 
} 

和我的包裹構造看起來像這樣

私人TransactionHistoryDe​​tail1(包裹中){

SimpleDateFormat sdf = new SimpleDateFormat(JsonBuilder.DateFormat); 
    try { 
     TxLclDtTm = sdf.parse(in.readString()); 
     UpdtDt = sdf.parse(in.readString()); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    id = in.readLong(); 
    TxUd = in.readString(); 
    PdSrl = in.readString(); 
    PdBrndDsc = in.readString(); 
    UsrEml = in.readString(); 
    CstmrAdr = in.readString(); 
    TxDtlDsc = in.readString(); 
    TxRltdInd = in.readByte() != 0; 
    Amt = new BigDecimal(in.readDouble()); 
    CcyCd = CurrencyCode.valueOf(in.readString()); 
    PmttpCd = PaymentTypeCode.valueOf(in.readString()); 
    OprtnCd = OperationCode.valueOf(in.readString()); 
    AppLabltpCd = AppLabelTypeCode.valueOf(in.readString()); 
    TxSttsCd = TransactionStatusCode.fromValue(in.readString()); 

} 

的writeToParcel我相信的其工作好吧,但構造函數崩潰在「CcyCd」行。

我的貨幣代碼類是一個枚舉,(所以都PaymentTypeCode,操作代碼和AppLabelTypeCode),看起來像這樣:

@XmlType(name = "CurrencyCode") 
@XmlEnum 
public enum CurrencyCode { 

    EUR; 

    public String value() { 
     return name(); 
    } 

    public static CurrencyCode fromValue(String v) { 
     return valueOf(v); 
    } 
} 

有另一種方式來應對Parcelables並枚舉?

的例外,我得到的是:

了java.lang.RuntimeException:無法啓動活動ComponentInfo {包/ package.activities.ChildActivity}:java.lang.IllegalArgumentException異常:無枚舉常量package.data .apiClasses.CurrencyCode.EURCASHCLSAPP2017-04-10T09:07:52.525Z2017-04-10T09 :07:52.528ZCHILD_12345?

回答

1

你應該從Parcel在你寫它的確切的順序閱讀的第一位。由於包裹只是爲了寫入和讀取數據,而不是實際的數據序列化的,你必須保持閱讀,爲了別人,你會讀出錯誤的價值觀,並得到錯誤寫..

它是最好的,如果你把Enumint,寫他們parcel這樣的:

dest.writeInt(this.CcyCd == null ? -1 : this.CurrencyCode.ordinal()); 

像這樣閱讀:

int tmpCurrencyCode = in.readInt(); 
this.CcyCd = tmpCurrencyCode == -1 ? null : CurrencyCode.values()[tmpMState]; 

PS:此代碼檢查空值過^^

+1

什麼是tmpMState? – Ric

+0

林猜測它的tmpCurrencyCode。在in.readInt()已經有0,101,24等等之後,我在tmpCurrencyCode中獲得了一些奇怪的值。在這之後,我得到一個異常的行下面無法啓動活動ComponentInfo {package/package.activities.ChildActivity}:java.lang.ArrayIndexOutOfBoundsException:length = 1; index = 24 – Ric

+0

你是否按照閱讀和書寫包裹的順序? 你應該完全按照你寫給它的順序閱讀包裹,但是在你的代碼中,你不要按順序去做。 –