這是Android implement Parcelable object which has hashmap的一個擴展,但我的實驗有點不同。Android實現包含另一個散列映射的包含散列映射的可包含對象
我有這些類
public class EventDetails implements Parcelable {
Private String id;
Private String eventName;
Private Long eventUnixTime;
Private HashMap <String, User> pickedUser = null;
}
和
public class User implements Parcelable {
private String email;
private String userName;
private String userPicture;
private Boolean hasLoggedInWithPassword;
private HashMap<String, Object> dateJoined = null;
public User() {
}
public User(String email, String userName, String userPicture, Boolean hasLoggedInWithPassword, HashMap<String, Object> 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<User> CREATOR = new Creator<User>() {
@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<String, Object> 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<String, Object> 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<String, User> 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
嘗試使用代替Parcelable序列化。 –
@HareshChhelana爲什麼可序列化? –
@RissmonSuresh不需要編寫代碼進行編組和解組。 –