如何分配对象

时间:2016-09-24 18:18:40

标签: android object arraylist parcelable

我正在尝试学习parcelable。早些时候我正在使用get和set函数但是现在我使用parcelable将对象传递给其他类我制作了passdata类并实现了parcelable但是如何将列表添加到类中对象

class Passdata implements Parcelable {
private String title,album;
private long duration;
private Uri data,album_art;

public String getTitle() {
    return title;
}

void setTitle(String title) {
    this.title = title;
}

public String getAlbum() {
    return album;
}

void setAlbum(String album) {
    this.album = album;
}

public long getDuration() {
    return duration;
}

void setDuration(long duration) {
    this.duration = duration;
}

public Uri getData() {
    return data;
}

void setData(Uri data) {
    this.data = data;
}

public Uri getAlbum_art() {
    return album_art;
}

void setAlbum_art(Uri album_art) {
    this.album_art = album_art;
}

Passdata(Parcel in) {
    title = in.readString();
    album = in.readString();
    duration = in.readLong();
    data = in.readParcelable(Uri.class.getClassLoader());
    album_art = in.readParcelable(Uri.class.getClassLoader());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(title);
    dest.writeString(album);
    dest.writeLong(duration);
    dest.writeParcelable(data, flags);
    dest.writeParcelable(album_art, flags);
}

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

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

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

如何添加像这样的对象

  do {
                    int audioTitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                    int audioartist = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
                    int audioduration = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);
                    int audiodata = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                    int audioalbum = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
                    int audioalbumid = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);
                    Passdata info = new Passdata();
                    info.setData(Uri.parse(audioCursor.getString(audiodata)));
                    info.setTitle(audioCursor.getString(audioTitle));
                    info.setDuration(audioCursor.getLong(audioduration));
                   // info.setArtist(audioCursor.getString(audioartist));
                    info.setAlbum(audioCursor.getString(audioalbum));
                    info.setAlbum_art(ContentUris.withAppendedId(albumArtUri, audioCursor.getLong(audioalbumid)));
                    audioList.add(info);
                } 

我在创建passdata对象时遇到错误 如何解决这个问题??

1 个答案:

答案 0 :(得分:1)

PassData课程中没有空构造函数。添加以下代码:

public Passdata() {

}