2016-07-03 18 views
10

Đây là lớp mô hình của tôi:Làm thế nào để đọc và viết Enum thành bưu kiện trên Android?

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 là 'Serializable'. – CommonsWare

+1

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

+0

bạn đã thử 'dest.writeValue()' – EpicPandaForce

Trả lời

25

Tôi có vấn đề tương tự và giải pháp của tôi là:

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

và cho việc đọc:

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

QuestionType là enum và nhớ rằng Trật tự của yếu tố quan trọng.

2

Enum decleration:

public enum Action { 

    NEXT(1), 
    OK(2); 

    private int action; 

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

} 

Đọc từ Parcel:

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

Viết để bưu kiện:

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

Bất kỳ enum là serializable.

Bạn có thể làm điều này trong writeToParcel(): dest.writeSerializable(action)

Và trong constructor: action = (Action) in.readSerializable()

+1

Đây là giải pháp dễ nhất để thực hiện nhưng xem xét giao diện 'serializable' chậm hơn nhiều so với' parcelable', tôi tự hỏi liệu điều này có bất kỳ ảnh hưởng đến hiệu suất so với các câu trả lời bên dưới nơi bạn truyền 'enum' thành kiểu nguyên thủy hơn. – mcy

0

Các hiệu quả nhất - bộ nhớ hiệu quả - bó được tạo ra bằng cách sử dụng các giá trị thứ ENUM.

Viết để Parcel dest.writeInt(enum_variable.ordinal());

Đọc từ bưu kiện enum_variable = EnumName.values()[in.readInt()];

này nên được tốt, trừ khi bạn không chú ý đến lời nhắc nhở của tài liệu:

Parcel không phải là một cơ chế serialization có mục đích chung. Lớp này (và Parcelable API tương ứng để đặt các đối tượng tùy ý vào một Parcel) được thiết kế như là một vận chuyển IPC hiệu suất cao. Như vậy, không thích hợp để đặt bất kỳ dữ liệu bưu kiện nào vào lưu trữ liên tục: các thay đổi trong việc triển khai cơ bản của bất kỳ dữ liệu nào trong Bưu kiện có thể làm cho dữ liệu cũ không thể đọc được.

Nói cách khác, bạn không nên chuyển bưu kiện giữa các phiên bản mã vì nó có thể không hoạt động.

Các vấn đề liên quan