SAVVY SAVVY - 2 months ago 7
Android Question

How to parcelable object

I am trying to learn parcelable .Earlier I was using get and set function but now I am using parcelable to pass the object to other class I made the passdata class and implemented the parcelable but how I can add the list to the class object.

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


how can I add the object like this

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


I am getting error in the place were I am creating the passdata object
how to fix this??

Answer

You don't have empty constructor in PassData class. Add this code:

public Passdata() {

}
Comments