本文介绍了如何实现Parcelable含名单,其中一类;列表<字符串>>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作 Parcelable implementaion所有在我Parcelable类中的字段除了名单,其中,名单,其中,字符串>>

  Employee类实现Parcelable {

    名单<列表<字符串>>细节;
    // .......

    保护员工(包裹的){
        细节=新的ArrayList<列表<字符串>>();
        //我知道这是错的只是发布澄清
        in.readList(细节,List.class.getClassLoader());
        // ......
    }

    公共无效writeToParcel(包裹DEST,INT标志){
        dest.writeList(详情);
        // .....
    }

    公众诠释describeContents(){
        返回0;
    }

    公共静态最终Parcelable.Creator<员工> CREATOR =
            新Parcelable.Creator<员工>(){
        公务员createFromParcel(包裹中){
            返回新员工(中);
        }

        公务员[] newArray(INT尺寸){
            返回新员工【尺寸】;
        }
    };

}
 

例外:

  05-10 19:07:44.072:E / AndroidRuntime(10661):java.lang.RuntimeException的:地块android.os.Parcel@42a509e8:由解组造成未知类型$ C $Ç3604535偏移量268
 

解决方案

扩展的ArrayList 和实施 Parcelable 就可以了为我工作。

 公共类ParcelableArrayList扩展的ArrayList<字符串>器物
        Parcelable {

    私有静态最后长的serialVersionUID = -8516873361351845306L;

    公共ParcelableArrayList(){
        超();
    }

    保护ParcelableArrayList(包裹中){
        in.readList(此,String.class.getClassLoader());
    }

    @覆盖
    公众诠释describeContents(){
        返回0;
    }

    @覆盖
    公共无效writeToParcel(包裹DEST,INT标志){
        dest.writeList(本);
    }

    公共静态最终Parcelable.Creator< ParcelableArrayList> CREATOR =
            新Parcelable.Creator< ParcelableArrayList>(){
        公共ParcelableArrayList createFromParcel(包裹中){
            返回新ParcelableArrayList(中);
        }

        公共ParcelableArrayList [] newArray(INT尺寸){
            返回新ParcelableArrayList【尺寸】;
        }
    };

}
 

和Employee类

  Employee类实现Parcelable {

    名单< ParcelableArrayList>细节;
    // .......

    保护员工(包裹的){
        细节=新的ArrayList< ParcelableArrayList>();
        in.readTypedList(详细信息,ParcelableArrayList.CREATOR);
        // ......
    }

    公共无效writeToParcel(包裹DEST,INT标志){
        dest.writeList(详情);
        // .....
    }

    公众诠释describeContents(){
        返回0;
    }

    公共静态最终Parcelable.Creator<员工> CREATOR =
            新Parcelable.Creator<员工>(){
        公务员createFromParcel(包裹中){
            返回新员工(中);
        }

        公务员[] newArray(INT尺寸){
            返回新员工【尺寸】;
        }
    };

}
 

I have a working Parcelable implementaion for all the fields in my Parcelable Class apart from List<List<String>>

class Employee implements Parcelable {

    List<List<String>> details;
    //.......

    protected Employee(Parcel in) {
        details = new ArrayList<List<String>>();
        // i know this is wrong just posting to clarify
        in.readList(details, List.class.getClassLoader());
        //......
    }

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

    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<Employee> CREATOR = 
            new Parcelable.Creator<Employee>() {
        public Employee createFromParcel(Parcel in) {
            return new Employee(in);
        }

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

}

Exception:

05-10 19:07:44.072: E/AndroidRuntime(10661): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@42a509e8: Unmarshalling unknown type code 3604535 at offset 268
解决方案

Extending ArrayList and implementing Parcelable on it worked for me.

public class ParcelableArrayList extends ArrayList<String> implements 
        Parcelable {

    private static final long serialVersionUID = -8516873361351845306L;

    public ParcelableArrayList(){
        super();
    }

    protected ParcelableArrayList(Parcel in) {
        in.readList(this, String.class.getClassLoader());
    }

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

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

    public static final Parcelable.Creator<ParcelableArrayList> CREATOR = 
            new Parcelable.Creator<ParcelableArrayList>() {
        public ParcelableArrayList createFromParcel(Parcel in) {
            return new ParcelableArrayList(in);
        }

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

}

and Employee class

class Employee implements Parcelable {

    List<ParcelableArrayList> details;
    //.......

    protected Employee(Parcel in) {
        details = new ArrayList<ParcelableArrayList>();
        in.readTypedList(details,ParcelableArrayList.CREATOR);
        //......
    }

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

    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<Employee> CREATOR = 
            new Parcelable.Creator<Employee>() {
        public Employee createFromParcel(Parcel in) {
            return new Employee(in);
        }

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

}

这篇关于如何实现Parcelable含名单,其中一类;列表&LT;字符串&GT;&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:47