自定义对象的扩展(自定义)arrayList parcelable返回大小为0

时间:2017-01-03 18:38:48

标签: java android-activity arraylist parcelable custom-object

有一天工作没有解决方案, 我想在活动之间传递数据,我实现了Parcelable但是从被调用的活动中我收到了一个size = 0的ListFileMedia。

当我弄错了?

类ListFileMedia(ArrayList的扩展)

public class ListFileMedia extends ArrayList<FileMedia> implements Parcelable{

public ListFileMedia(){.....} 

// for test我已取消所有方法和var,确定我也测试了...

 //Interface Parcelable

@Override
public int describeContents() {
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    //dest.writeInt(countIntestazioni);
    // Add inner class
    dest.writeParcelable(this.add(FileMedia),flags);
}

public ListFileMedia(Parcel in) {

}

public final static Parcelable.Creator <ListFileMedia>CREATOR = new Creator<ListFileMedia>() {

    @Override
    public ListFileMedia createFromParcel(Parcel source) {

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

        return new ListFileMedia[size];
    }
};
}

FileListMedia

中的自定义对象
public class FileMedia implements Comparable,Parcelable{


public String getPath() {return path;}

public String getBucket() {
    return bucket;
}

public String getMimeType() { return mimeType; }

private String path;
private String bucket;
private String mimeType;
private int giorno;
private int mese;
private int anno;

public FileMedia(int giorno,int mese,int anno, String path,String bucket,String mimeType){
....
}

public int compareTo(Object o) {
.....
}

  //Interface Parcelable

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.path);
    dest.writeString(this.bucket);
    dest.writeString(this.mimeType);
    dest.writeInt(this.giorno);
    dest.writeInt(this.mese);
    dest.writeInt(this.anno);
}
public FileMedia(Parcel in) {
    this.path = in.readString();
    this.bucket=in.readString();
    this.mimeType=in.readString();
    this.giorno=in.readInt();
    this.mese=in.readInt();
    this.anno=in.readInt();
}
public final static Parcelable.Creator <FileMedia>CREATOR = new Creator<FileMedia>() {

    @Override
    public FileMedia createFromParcel(Parcel source) {
        return new FileMedia(source);
    }

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

致电活动

ListFileMedia test=new ListFileMedia();
        ArrayList arrayList=new ArrayList();
        test.add(new FileMedia(10,12,2016,"t","t","t"));
        b.putParcelable("list",test);
        intent.putExtras(b);
        startActivity(intent);

被叫活动

Intent intent = this.getIntent();
       Bundle b=intent.getExtras();


ListFileMedia listFileMedia (ListFileMedia)b.getParcelable("list"); //size==0  !!!

但如果我只通过了b.putParcelable("list",new FileMedia(10,12,2016,"t","t","t")工作!!

抱歉我的英文..

0 个答案:

没有答案