如何在具有内部类的类中实现parcelable

时间:2016-02-18 13:11:31

标签: android android-studio parcelable

这个类有一个内部类,那么如何用内部类包裹一个类?

public class UserModel 
{
    private User user;

    public User getUser() {
        return user;
    }

    public class User {
        @SerializedName("id")
        @Expose
        private int id;

        @SerializedName("first_name")
        @Expose
        private String first_name;

        @SerializedName("last_name")
        @Expose
        private String last_name;

        @SerializedName("email")
        @Expose
        private String email;

        @SerializedName("mobile_no")
        @Expose
        private String mobile_no;

        @SerializedName("created_at")
        @Expose
        private String created_at;

        @SerializedName("updated_at")
        @Expose
        private String updated_at;

        @SerializedName("user_role_id")
        @Expose
        private int user_role_id;

        public int getId() {
            return id;
        }

        public String getFirst_name() {
            return first_name;
        }

        public String getLast_name() {
            return last_name;
        }

        public String getEmail() {
            return email;
        }

        public String getMobile_no() {
            return mobile_no;
        }

        public String getCreated_at() {
            return created_at;
        }

        public String getUpdated_at() {
            return updated_at;
        }

        public int getUser_role_id() {
            return user_role_id;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您必须使内部类可以是Parcelable或Serializable

public class UserModel implements Parcelable {

    private User user;
    public User getUser() {
        return user;
    }

    public class User implements Serializable{


        @SerializedName("id")
        @Expose
        private int id;

        @SerializedName("first_name")
        @Expose
        private String first_name;

        @SerializedName("last_name")
        @Expose
        private String last_name;

        @SerializedName("email")
        @Expose
        private String email;

        @SerializedName("mobile_no")
        @Expose
        private String mobile_no;

        @SerializedName("created_at")
        @Expose
        private String created_at;

        @SerializedName("updated_at")
        @Expose
        private String updated_at;

        @SerializedName("user_role_id")
        @Expose
        private int user_role_id;

        public int getId() {
            return id;
        }

        public String getFirst_name() {
            return first_name;
        }

        public String getLast_name() {
            return last_name;
        }

        public String getEmail() {
            return email;
        }

        public String getMobile_no() {
            return mobile_no;
        }

        public String getCreated_at() {
            return created_at;
        }

        public String getUpdated_at() {
            return updated_at;
        }

        public int getUser_role_id() {
            return user_role_id;
        }
    }

    protected UserModel(Parcel in) {
        user = (User) in.readValue(User.class.getClassLoader());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(user);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<UserModel> CREATOR = new Parcelable.Creator<UserModel>() {
        @Override
        public UserModel createFromParcel(Parcel in) {
            return new UserModel(in);
        }

        @Override
        public UserModel[] newArray(int size) {
            return new UserModel[size];
        }
    };
}

答案 1 :(得分:-1)

您希望这两个类都实现Parcelable接口。

资料来源:this