2017-03-17 111 views
0

这是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

+1

尝试使用代替Parcelable序列化。 –

+0

@HareshChhelana为什么可序列化? –

+2

@RissmonSuresh不需要编写代码进行编组和解组。 –

回答

1

的Android工作室已经提供插件(Android Parcelable code generator)自动生成Parcelable方法。

对于使用上述插件我已创建下面的类与Parcelable自动生成的方法

public class EventDetails implements Parcelable { 

    private String id; 
    private String eventName; 
    private Long eventUnixTime; 
    private HashMap<String, User> 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<String, User>) in.readSerializable(); 
    } 

    public static final Creator<EventDetails> CREATOR = new Creator<EventDetails>() { 
     @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<String, Object> dateJoined = null; 

    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; 
    } 


    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() { 
     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<String, Object>) in.readSerializable(); 
    } 

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

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

谢谢Haresh。我会试试这个。 – runrmrf

+1

这是不正确的,它将在编组时抛出一个'java.io.NotSerializableException'。你的User类没有实现Serializable。如果对象的一部分不是可序列化的(即'用户'),则不能指望编写可序列化的对象。 此外,你不能简单地''用户'实现'Serializable'并期望它的工作。您将需要使用不同的数据结构(例如'List '),否则您将需要自行处理从/到/到一个Parcel的读写。 – w3bshark