【问题标题】:How to implement Parcelable for a class containing List<List<String>>?如何为包含 List<List<String>> 的类实现 Parcelable?
【发布时间】:2013-05-11 02:11:04
【问题描述】:

除了List&lt;List&lt;String&gt;&gt; 之外,我的 Parcelable 类中的所有字段都有一个有效的 Parcelable 实现

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];
        }
    };

}

例外:

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

【问题讨论】:

    标签: android parcelable


    【解决方案1】:

    扩展 ArrayList 并在其上实施 Parcelable 对我有用。

    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];
            }
        };
    
    }
    

    和员工类

    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];
            }
        };
    
    }
    

    【讨论】:

      【解决方案2】:

      我将创建一个扩展 List 的类,在该类上实现 Parcelable。否则,您可以将其视为普通列表,但允许它是可打包的。

      【讨论】:

        【解决方案3】:

        创建包含List&lt;String&gt;class DetailsEntry implements Parcelable 并在Employee 中使用List&lt;DetailsEntry&gt; details

        【讨论】:

          猜你喜欢
          • 2021-01-13
          • 2012-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-09
          • 2020-01-11
          • 2013-08-26
          • 2020-11-27
          相关资源
          最近更新 更多