2016-07-03 7 views
10

यहाँ मेरी मॉडल वर्ग है:Android पर पार्सल में Enum को कैसे पढ़ और लिखना है?

public enum Action { 
    RETRY, SETTINGS 
} 

private int imageId; 
private String description; 
private String actionName; 
private Action action; 

public NetworkError(int imageId, String description, String actionName, Action action) { 
    this.imageId = imageId; 
    this.description = description; 
    this.actionName = actionName; 
    this.action = action; 
} 

public int getImageId() { 
    return imageId; 
} 

public void setImageId(int imageId) { 
    this.imageId = imageId; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public String getActionName() { 
    return actionName; 
} 

public void setActionName(String actionName) { 
    this.actionName = actionName; 
} 


@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeInt(this.imageId); 
    dest.writeString(this.description); 
    dest.writeString(this.actionName); 
} 

protected NetworkError(Parcel in) { 
    this.imageId = in.readInt(); 
    this.description = in.readString(); 
    this.actionName = in.readString(); 
} 

public static final Parcelable.Creator<NetworkError> CREATOR = new Parcelable.Creator<NetworkError>() { 
    @Override 
    public NetworkError createFromParcel(Parcel source) { 
     return new NetworkError(source); 
    } 

    @Override 
    public NetworkError[] newArray(int size) { 
     return new NetworkError[size]; 
    } 
}; 
+3

Enums 'Serializable' हैं। – CommonsWare

+1

http://www.parcelabler.com/ – tachyonflux

+0

क्या आपने 'dest.writeValue()' – EpicPandaForce

उत्तर

25

मैं इसी तरह की समस्या थी और मेरे समाधान किया गया था:

parcel.writeString(this.questionType.name()); 

और पढ़ने के लिए:

this.questionType = QuestionType.valueOf(parcel.readString()); 

QuestionType enum है और उस आदेश को याद तत्व मायने रखता है।

2

Enum decleration:

public enum Action { 

    NEXT(1), 
    OK(2); 

    private int action; 

    Action(int action) { 
     this.action = action; 
    } 

} 

पार्सल से पढ़ रहा है:

protected ActionParcel(Parcel in) { 
    int actionTmp = in.readInt(); 
    action = Tutorials.Action.values()[actionTmp]; 
} 

पार्सल करने के लिए लेखन:

public void writeToParcel(Parcel dest, int flags) { 
    int actionTmp = action == null ? -1 : action.ordinal(); 
    dest.writeInt(actionTmp); 
} 
4

किसी भी enum serializable है।

आप writeToParcel() में ऐसा कर सकते हैं: dest.writeSerializable(action)

और निर्माता में: action = (Action) in.readSerializable()

+1

को कार्यान्वित करने का प्रयास किया है, लेकिन यह लागू करने का सबसे आसान समाधान है, लेकिन 'serializable' इंटरफेस पर विचार करना 'पार्ससेलबल' से बहुत धीमा है, मुझे आश्चर्य है कि इसमें कोई है या नहीं नीचे दिए गए उत्तरों की तुलना में प्रदर्शन पर प्रभाव जहां आपने 'enum' को एक और आदिम प्रकार में डाला था। – mcy

0

सबसे कुशल - स्मृति कुशल - बंडल Enum क्रमसूचक मूल्य का उपयोग करके बनाया जाता है।

पार्सल enum_variable = EnumName.values()[in.readInt()];

से पढ़ना dest.writeInt(enum_variable.ordinal());

पार्सल करने के लिए लेखन जब तक आप प्रलेखन के admonitions ध्यान नहीं है यह ठीक किया जाना चाहिए:

पार्सल एक सामान्य प्रयोजन क्रमबद्धता तंत्र नहीं मानता। इस वर्ग (और पार्सल में मनमानी वस्तुओं को रखने के लिए संबंधित पार्ससेल योग्य एपीआई) को उच्च प्रदर्शन वाले आईपीसी परिवहन के रूप में डिजाइन किया गया है। इस प्रकार, किसी भी पार्सल डेटा को लगातार भंडारण में रखना उचित नहीं है: पार्सल में किसी भी डेटा के अंतर्निहित कार्यान्वयन में परिवर्तन पुराने डेटा को अपठनीय प्रस्तुत कर सकते हैं।

दूसरे शब्दों में, आपको कोड संस्करणों के बीच पार्सल पास नहीं करना चाहिए क्योंकि यह काम नहीं कर सकता है।

संबंधित मुद्दे