Problem unmarshalling parcelables

后端 未结 10 1956
旧时难觅i
旧时难觅i 2020-11-27 15:45

I\'ve got a few classes that implement Parcelable and some of these classes contain each other as properties. I\'m marshalling the classes into a Parcel to pass them between

10条回答
  •  醉话见心
    2020-11-27 16:20

    If you have an Object with a property of type of List objects you should pass the class loader when you read the property for example:

    public class Mall implements Parcelable {
    
        public List getOutstanding() {
            return outstanding;
        }
    
        public void setOutstanding(List outstanding) {
            this.outstanding = outstanding;
        }        
    
        protected Mall(Parcel in) {
            outstanding = new ArrayList();
            //this is the key, pass the class loader
            in.readList(outstanding, Outstanding.class.getClassLoader());
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeList(outstanding);
        }
    
        public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
            public Mall createFromParcel(Parcel in) {
                return new Mall(in);
            }
    
            public Mall[] newArray(int size) {
                return new Mall[size];
            }
        };
    }
    

    Note: Is important that the class Outstanding implements the Parceable interface.

提交回复
热议问题