Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement parcelable for List<Long>

Tags:

android

I'm trying to pass a List in my parcelable doing:

public class MetaDados implements Parcelable {

private List<Long> sizeImages;

public MetaDados(List<Long> sizeImages){
this.sizeImages = sizeImages;
}

public List<Long> getSizeImages() {
        return sizeImages;
    }

    public void setSizeImages(List<Long> sizeImages) {
        this.sizeImages = sizeImages;
    }

@Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

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

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

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

private MetaDados(Parcel in) {
//HERE IS THE PROBLEM, I'VE tried this:

sizeImages = in.readList(sizeImages, Long.class.getClassLoader());

//but what i got: Type mismatch: cannot convert from void to List<Long>
}
}
like image 592
Victor Laerte Avatar asked Apr 02 '13 20:04

Victor Laerte


1 Answers

Try this instead:

sizeImages = new ArrayList<Long>(); // or any other type of List
in.readList(sizeImages, null);

The Android documentation for Parcel.readList says:

Read into an existing List object from the parcel

and thus, you need to first create the List.

like image 146
Tushar Avatar answered Oct 21 '22 23:10

Tushar