Android使用包含另一个hashmap的hashmap实现Parcelable对象

这是Android实现Parcelable对象的扩展, 它有hashmap但我的有点不同。

我有这些课程

public class EventDetails implements Parcelable { Private String id; Private String eventName; Private Long eventUnixTime; Private HashMap  pickedUser = null; } 

 public class User implements Parcelable { private String email; private String userName; private String userPicture; private Boolean hasLoggedInWithPassword; private HashMap dateJoined = null; public User() { } public User(String email, String userName, String userPicture, Boolean hasLoggedInWithPassword, HashMap dateJoined) { this.email = email; this.userName = userName; this.userPicture = userPicture; this.hasLoggedInWithPassword = hasLoggedInWithPassword; this.dateJoined = dateJoined; } protected User(Parcel in) { email = in.readString(); userName = in.readString(); userPicture = in.readString(); int size = in.readInt(); for (int i = 0; i < size; i++) { String key = in.readString(); Object value = in.readString(); dateJoined.put(key, value); } } public static final Creator CREATOR = new Creator() { @Override public User createFromParcel(Parcel in) { return new User(in); } @Override public User[] newArray(int size) { return new User[size]; } }; public String getEmail() { return email; } public String getUserName() { return userName; } public String getUserPicture() { return userPicture; } public Boolean getHasLoggedInWithPassword() { return hasLoggedInWithPassword; } public HashMap getDateJoined() { return dateJoined; } @Override public int describeContents() { Log.i("describeUser", "describe content from User.java"); return 0; } @Override public void writeToParcel(Parcel dest, int flags) { Log.i("write to Parcel", "write to Parcel from User.java"); dest.writeString(email); dest.writeString(userName); dest.writeString(userPicture); dest.writeInt(hasLoggedInWithPassword ? 1:0); dest.writeInt(dateJoined.size()); for (HashMap.Entry entry : dateJoined.entrySet()) { dest.writeString(entry.getKey()); dest.writeString(String.valueOf(entry.getValue())); } } } 

我在EventDetails类中遇到了这些方法:

 protected EventDetails(Parcel in) { id = in.readString(); eventName = in.readString(); eventUnixTime = in.readLong(); final int size = in.readInt(); for (int i = 0; i < size; i++) { String key = in.readString(); User user = in.readHashMap(userMap); pickedFriendsHashMap.put(key, user); } } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(pickedFriendsHashMap.size()); for (HashMap.Entry entry : pickedFriendsHashMap.entrySet()) { dest.writeString(entry.getKey()); dest.writeInt(listLength); for (User user: userList) { dest.writeParcelable(user, 0); } } dest.writeString(id); dest.writeString(eventName); dest.writeLong(eventUnixTime); } 

请告知我如何正确地将这些类放入包裹并发送包。

谢谢。

-R

Android studio已经提供了插件(Android Parcelable code generator)来自动生成Parcelable方法。

对于使用上面的插件,我使用Parcelable自动生成的方法创建了以下类

 public class EventDetails implements Parcelable { private String id; private String eventName; private Long eventUnixTime; private HashMap pickedUser; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.id); dest.writeString(this.eventName); dest.writeValue(this.eventUnixTime); dest.writeSerializable(this.pickedUser); } public EventDetails() { } protected EventDetails(Parcel in) { this.id = in.readString(); this.eventName = in.readString(); this.eventUnixTime = (Long) in.readValue(Long.class.getClassLoader()); this.pickedUser = (HashMap) in.readSerializable(); } public static final Creator CREATOR = new Creator() { @Override public EventDetails createFromParcel(Parcel source) { return new EventDetails(source); } @Override public EventDetails[] newArray(int size) { return new EventDetails[size]; } }; } public class User implements Parcelable { private String email; private String userName; private String userPicture; private Boolean hasLoggedInWithPassword; private HashMap dateJoined = null; public User(String email, String userName, String userPicture, Boolean hasLoggedInWithPassword, HashMap dateJoined) { this.email = email; this.userName = userName; this.userPicture = userPicture; this.hasLoggedInWithPassword = hasLoggedInWithPassword; this.dateJoined = dateJoined; } public String getEmail() { return email; } public String getUserName() { return userName; } public String getUserPicture() { return userPicture; } public Boolean getHasLoggedInWithPassword() { return hasLoggedInWithPassword; } public HashMap getDateJoined() { return dateJoined; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.email); dest.writeString(this.userName); dest.writeString(this.userPicture); dest.writeValue(this.hasLoggedInWithPassword); dest.writeSerializable(this.dateJoined); } protected User(Parcel in) { this.email = in.readString(); this.userName = in.readString(); this.userPicture = in.readString(); this.hasLoggedInWithPassword = (Boolean) in.readValue(Boolean.class.getClassLoader()); this.dateJoined = (HashMap) in.readSerializable(); } public static final Creator CREATOR = new Creator() { @Override public User createFromParcel(Parcel source) { return new User(source); } @Override public User[] newArray(int size) { return new User[size]; } }; }