Sending Nested Parcelable with Intent

烂漫一生 提交于 2019-12-02 01:50:21

You have to instanciate your ArrayList< B > in :

 private A (Parcel in){
    var = new ArrayList<B>();
    in.readTypedList(this.var, B.CREATOR);
}

Say if it works :)

You are not assigning data to var variable in constructor.

change it like

private A (Parcel in){

    var=(ArrayList)in.readTypedList(this.var, B.CREATOR);
}

Edit::

Change your code like this and try

@Override
public void writeToParcel(Parcel dest, int flags) {

    B[] data = new B[var.size()];
    for (int i = 0; i < data.length; i++) {
        data[i] = var.get(i);
    }
    dest.writeParcelableArray(data, flags);

}

public A(Parcel in) {
    Parcelable[] parcelables = in.readParcelableArray(Thread
            .currentThread().getContextClassLoader());
    ArrayList<B> list = new ArrayList<B>();
    for (Parcelable parcelable : parcelables) {
        list.add((B) parcelable);
    }
    var = list;
}

you have problem with parsing the arraylist... do these changes..

private A (Parcel in){
        in.readTypedList(this.var, B.CREATOR);
}

to

this.var=in.readArrayList(B.class.getClassLoader());

and

public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(this.var);
}

to

dest.writeList(this.var);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!