2013-04-02 48 views
7

我試圖通過一個列表,我parcelable這樣做的:如何實現parcelable的列表<Long>

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

回答

15

試試這個:

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

Android documentation for Parcel.readList說:

從包裹中讀入現有列表對象

因此,您需要先創建List

相關問題