Android的Parcelable - 写和读的ArrayList< IA>当IA是一个接口 [英] Android Parcelable - Write and read ArrayList< IA > when IA is a interface

查看:523
本文介绍了Android的Parcelable - 写和读的ArrayList< IA>当IA是一个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口的 IA 和阶级的 C 实现它们。
两个B和C​​实施 Parcelable 以及

然后,我有棘手的问题:

类的 D 有一个的ArrayList< IA> 。我需要这在的ArrayList 太插入两个类B和C。它们共享相同的结构,但在是一个的关系不适用。

我需要从一个活动ð传递到另一个地块。

我试着写(ArrayList的< IA>)in.readSerializable ,但我得到了一个 IOException异常 。我知道,如果IA不是一个接口问题很容易,但我似乎无法找到这个简单的解决办法。

任何想法?

  @燮pressWarnings(未登记)
公共D(包裹中){
    名单=新的ArrayList< IA>();
    (......)
    名单=(ArrayList的< IA>)in.readSerializable
    }@燮pressWarnings(rawtypes)
公共静态最终Parcelable.Creator CREATOR =
新Parcelable.Creator(){
    公共ðcreateFromParcel(包裹中){
        返回(以)新的D;
    }    公众D [] newArray(INT大小){
        返回新的D [大小];
    }
};公众诠释describeContents(){
    返回0;
}公共无效writeToParcel(DEST包裹,INT标志){
    (......)
    dest.writeList(名单);
}


解决方案

  @燮pressWarnings(未登记)
公共D(包裹中){
    名单=新的ArrayList< IA>();
    (......)
    //错误 - >名单=(ArrayList的< IA>)in.readSerializable
    清单= in.readArrayList(IA.class.getClassLoader());
}@燮pressWarnings(rawtypes)
公共静态最终Parcelable.Creator CREATOR =
新Parcelable.Creator(){
    公共ðcreateFromParcel(包裹中){
        返回(以)新的D;
    }    公众D [] newArray(INT大小){
        返回新的D [大小];
    }
};公众诠释describeContents(){
    返回0;
}公共无效writeToParcel(DEST包裹,INT标志){
    (......)
    dest.writeList(名单);
}

I have a interface IA and class B and C that implement them. Both B and C implement Parcelable as well.

Then I have the tricky part:

Class D has a ArrayList< IA >. I need this too insert both classes B and C in the arraylist. They share the same structure but the "IS-A" relation don't apply.

I need to pass D from one activity to another as a Parcel.

I've tried to write (ArrayList<IA>) in.readSerializable but I got a IOException. I know that if IA was not a interface the problem was easy, but I can't seem to find an easy solution for this.

Any ideas?

@SuppressWarnings("unchecked")
public D (Parcel in) {
    list = new ArrayList<IA>();
    (...)    
    list = (ArrayList<IA>) in.readSerializable 
    }

@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
    public D createFromParcel(Parcel in) {
        return new D(in);
    }

    public D[] newArray(int size) {
        return new D[size];
    }
};

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    (...)
    dest.writeList(list);
}

解决方案

    @SuppressWarnings("unchecked")
public D (Parcel in) {
    list = new ArrayList<IA>();
    (...)    
    //ERROR -> list = (ArrayList<IA>) in.readSerializable 
    list = in.readArrayList(IA.class.getClassLoader());
}

@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
    public D createFromParcel(Parcel in) {
        return new D(in);
    }

    public D[] newArray(int size) {
        return new D[size];
    }
};

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    (...)
    dest.writeList(list);
}

这篇关于Android的Parcelable - 写和读的ArrayList&LT; IA&GT;当IA是一个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆