First off: consider following Java conventions and rename genericSongClass
to GenericSong
. The rest of this will assume you have not changed that name.
Second off: read this amazing guide at using/understanding Parcelables http://blog.logicexception.com/2012/09/a-parcelable-tutorial-for-android.html
Finally: some or all of the solution to your exact code:
Add a new constructor in genericSongClass
:
public genericSongClass(Parcel in) {
songTitle = in.readString();
// read in the rest of your values here
}
And add a default constructor:
public genericSongClass() {
}
The CREATOR
in genericSongClass
should look like this: (adjust Music
class as well)
public final static Parcelable.Creator<genericSongClass> CREATOR = new Parcelable.Creator<genericSongClass>() {
@Override
public genericSongClass createFromParcel(Parcel source) {
return new genericSongClass(source);
}
@Override
public genericSongClass[] newArray(int size) {
return new genericSongClass[size];
}
};
Adjust the writeToParcel
method:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(songTitle);
// write your other values here
}
KEEP IN MIND THAT ORDER IS IMPORTANT! Reads and Writes should be in the same order!
In your Music
class constructor:
in.readTypedList(songs, genericSongClass.CREATOR);
In your Music
class writeToParcel
:
dest.writeTypedList(songs);