如何创建包裹?

时间:2018-10-02 07:48:59

标签: java android

我有一个名为MusicFile的类,其构造函数为:

protected MusicFile(Parcel in) {
        Title = in.readString();
        Album = in.readString();
        Artist = in.readString();
        Path = in.readString();
        Genre = in.readString();
        Data = in.readString();
        artistID = in.readInt();
        if (in.readByte() == 0) {
            AlbumID = null;
        } else {
            AlbumID = in.readLong();
        }
        albumCover = in.readParcelable(Uri.class.getClassLoader());
    }

但是现在已经可以扩展Parcelable了,我不能像这样创建新的MusicFile

MusicFile musicFile = new MusicFile();

因为它需要Parcel In

如何立即创建新的音乐文件?我尝试了以下方法:

` 宗地宗地= New Parcel();

MusicFile musicFile = new MusicFile(parcel); `

但是那似乎不起作用。我收到以下错误:'Parcel(long)' is not public in 'android.os.Parcel'. Cannot be accessed from outside package

正确的方法是什么?

3 个答案:

答案 0 :(得分:1)

您可以扩展Parcelable ,并保留默认的构造函数

import android.os.Parcel;
import android.os.Parcelable;


public class MusicFile implements Parcelable {

    private Integer id;
    private String title;
    ...

    public MusicFile(Integer id, String title) {
        this.id = id;
        this.title = title;
    }

    public MusicFile(){

    }

    protected MusicFile(Parcel in) {
        id = in.readByte() == 0x00 ? null : in.readInt();
        title = in.readString();
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (id == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeInt(id);
        }
        dest.writeString(title);
    }

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

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

当您以Parcel之类的捆绑方式传递对象时,Android将处理bundle.addParcelableExtra(...)类。您不应尝试自己处理这些事情。当您调用addParcelableExtra()时,Android使用这些方法(writeToParceldescribeContents等)来序列化对象,但是这完全取决于框架。您应该添加这些方法来帮助Android自动序列化对象(并避免很多麻烦),但保留标准构造函数,getter / setter等。

答案 1 :(得分:0)

您需要为parcelable创建一个空的构造函数。您的模型现在应该看起来像这样:

 public static final Creator<MusicFile> CREATOR = new Creator<MusicFile>() {
    @Override
    public MusicFilecreateFromParcel(Parcel in) {
        return new MusicFile(in);
    }

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

// It's an empty constructor for parcelable 
private MusicFile() {

}

// This constructor is what parcel generates.
protected MusicFile(Parcel in) {
        Title = in.readString();
        Album = in.readString();
        Artist = in.readString();
        Path = in.readString();
        Genre = in.readString();
        Data = in.readString();
        artistID = in.readInt();
        if (in.readByte() == 0) {
            AlbumID = null;
        } else {
            AlbumID = in.readLong();
        }
        albumCover = in.readParcelable(Uri.class.getClassLoader());
    }

答案 2 :(得分:0)

您可以使用 Parcel.obtain() 获得 Parcel。别忘了做

<块引用>

parcel.recycle()

使用完毕后。