1

In my Application I have class which contains a List of List to which I want to make Parcelable to particular class so far I have tried

public class FeaturesParcelable implements Parcelable {
    private List<List<SpinnerModel>> featuresSublist;

    public List<List<SpinnerModel>> getFeaturesSublist() {
        return featuresSublist;
    }

    public void setFeaturesSublist(List<List<SpinnerModel>> featuresSublist) {
        this.featuresSublist = featuresSublist;
    }

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

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

    public FeaturesParcelable() {
    }

    protected FeaturesParcelable(Parcel in) {
        this.featuresSublist = new ArrayList<List<SpinnerModel>>();
        in.readList(this.featuresSublist, List<SpinnerModel>.class.getClassLoader());
    }

    public static final Parcelable.Creator<FeaturesParcelable> CREATOR = new Parcelable.Creator<FeaturesParcelable>() {
        @Override
        public FeaturesParcelable createFromParcel(Parcel source) {
            return new FeaturesParcelable(source);
        }

        @Override
        public FeaturesParcelable[] newArray(int size) {
            return new FeaturesParcelable[size];
        }
    };
}

but I getting an error cannot select from parameterized type inside

protected FeaturesParcelable(Parcel in) {
        this.featuresSublist = new ArrayList<List<SpinnerModel>>();
        in.readList(this.featuresSublist, List<SpinnerModel>.class.getClassLoader());
    }
Mehvish Ali
  • 622
  • 1
  • 9
  • 26
  • 1
    Because of how interfaces/generics work in Java, for starters you likely need a concrete List implementation rather than using the List interface as you are above. – Submersed Jan 30 '17 at 16:31

1 Answers1

1

1) List<SpinnerModel>.class does not resolve do the thing you expect. Java uses generic type erasure, so the run-time type of that statement is Class<List<?>>. This is not enough to identify suitable implementation of List or the type of the list element.

2) Even if you overcome your initial difficulty (e.g. by replacing List with ArrayList and manually deserializing nested lists), you should be aware, that List<SpinnerModel>.class.getClassLoader() returns a system ClassLoader. System ClassLoader is hardcoded to load only Android framework classes; in order to deserialize classes, declared in your application, use ClassLoader assigned to them: SpinnerModel.class.getClassLoader().

Community
  • 1
  • 1
user1643723
  • 3,634
  • 1
  • 20
  • 44