Android implement Parcelable objects with hashmap that contains another hashmap

坚强是说给别人听的谎言 提交于 2019-12-06 09:22:46

Android studio already provide plugin (Android Parcelable code generator) to generate Parcelable methods automatically.

For using above plugin i have create below classes with Parcelable auto generated methods

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];
        }
    };
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!