Android中的Activity之间传递自定义类型的List的方法

一、传递自定义类型
当Activity之间进行通信时,我们通常会遇到传递自定义类型,正如我们所知,自定义类型是无法直接通过Android中的Intent进行传递的,这时,我们可以采取将自定义类型实现Parcelable接口,并使用Intent的putExtras来进行传递。

以下是本人的一个数据类的实例:

public class UserData implements Parcelable{
    private String username;
    private String password;
    private String Email;
    private boolean logined;

    public UserData() {
        this.username = "";
        this.password = "";
        this.Email = "";
        this.logined = false;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(username);
        dest.writeString(password);
        dest.writeString(Email);
    }

    public UserData(String username, String password, String Email) {
        this.username = username;
        this.password = password;
        this.Email = "";
        this.logined = false;
    }

    public UserData(Parcel in) {
        username = in.readString();
        password = in.readString();
        Email = in.readString();
    }

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

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

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

    public String getUsername() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setLoginStatus(boolean status) {
        this.logined = status;
    }

    public boolean isLogined() {
        return this.logined;
    }

    public void setUsername( String username ) {
        this.username = username;
    }

    public void setPassword( String password ) {
        this.password = password;
    }

    public void setEmail( String email ) {
        this.Email = email;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + username + '\'' +
                ", password='" + password + '\'' +
                ", Email='" + Email + '\'' +
                '}';
    }
}
注意在实现接口时,一定要实现
public void writeToParcel(Parcel dest, int flags);
public int describeContents();

并且此类一定要包含一个静态的CREATOR(见代码)

如果这个类还包含一个自定义类型的对象,那么这个自定义的类型也应当实现Parcelable接口。

并且,在writeToParcel这个函数中使用writeParcelable方法来写入Parcel,在获取Parcel中的对象时(见代码的

public UserData(Parcel in) 构造函数),要使用readParcelable读取.


二、传递List的自定义类型
传递List的自定义类型与上方类似,只是在写入Parcel和读取时有所不同

本人有一个小小的实例,这是一个包含两个类型的例子:

Tag.java

public class Tag implements Parcelable{
    private String tag_name;
    private String tag_num;
    private String tag_url;

    public List<Article> articles = new ArrayList<>();

    @Override
    public void writeToParcel(
            Parcel dest,
            int flags
            ) {
        dest.writeString(this.getTag_name());
        dest.writeString(this.getTag_num());
        dest.writeString(this.getTag_url());
        dest.writeTypedArray(
                this.getArticles().toArray(new Article[this.getArticles().size()]),
                flags
        );
    }

    public Tag() {
        this.tag_url = "";
        this.tag_name = "";
        this.tag_num = "";
    }

    public Tag( String _tag_name, String _tag_num, String _tag_url) {
        this.tag_name = _tag_name;
        this.tag_num = _tag_num;
        this.tag_url = _tag_url;
    }

    public Tag( Parcel parcel ) {
        this.tag_name = parcel.readString();
        this.tag_num = parcel.readString();
        this.tag_url = parcel.readString();
        // Article[] temp = parcel.readParcelableArray(Article.class.getClassLoader());
        Article[] temp = parcel.createTypedArray(Article.CREATOR);

        this.articles = Arrays.asList(temp);
    }

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

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

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

    public void setTag_name( String name ) {
        this.tag_name = name;
    }

    public void setTag_num( String num ) {
        this.tag_num = tag_num;
    }

    public void setTag_url( String url ) {
        this.tag_url = url;
    }

    public String getTag_name() {
        return this.tag_name;
    }

    public String getTag_num() {
        return this.tag_num;
    }

    public String getTag_url() {
        return this.tag_url;
    }

    public List<Article> getArticles() {
        return articles;
    }

    public void setArticles(List<Article> articles) {
        this.articles = articles;
    }

    @Override
    public String toString() {
        String article = "";
        for (int i = 0; i < articles.size(); ++i) {
            article += articles.get(i).toString() + "\n";
        }
        return
                " : " + this.tag_num + " : " + this.tag_url + "\n" + article;
    }
}

Article.java

public class Article implements Parcelable{
    private String title;
    private String url;
    private String date;
    private String publisher;

    @Override
    public void writeToParcel(Parcel dest, int flags){
        dest.writeString(this.title);
        dest.writeString(this.url);
    }

    public Article() {
        this.title = "";
        this.url = "";
    }

    public Article(String _title, String _content) {
        this.title = _title;
        this.url = _content;
    }

    public Article( Parcel parcel ) {
        this.title = parcel.readString();
        this.url = parcel.readString();
    }

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

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

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

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    @Override
    public String toString() {
        return this.url;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值