2016-09-24 74 views
-2

我想學習parcelable。早先我使用get和set函數,但現在我使用parcelable將對象傳遞給其他類我做了passdata類並實現了parcelable,但是如何添加列表到類對象。如何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

我們看不到你的屏幕,「我得到錯誤」什麼錯誤? – 2016-09-24 18:20:10

+0

它顯示傳遞的數據不能在Android Studio中的插件應用 – SAVVY

+0

AURO parcelable插件 搜索它 https://github.com/mcharmas/android-parcelable-intellij-plugin – amorenew

回答

1

您在PassData類中沒有空的構造函數。加入了這段代碼:

public Passdata() { 

} 
+0

休息一切都很好?或者我犯了一些錯誤 – SAVVY

相關問題