Reputation: 131
In the class I am trying to make parcelable there is a smaller class called genericSongClass. In the larger class there is an ArrayList of genericSongClass's. So I know I have to make genericSongClass parcelable, but I cannot give it a CREATOR without making the class static. Is there any way I can do this?
My smaller class as it stands now in the current confusion:
public class genericSongClass implements Parcelable {
String songTitle = "";
String songArtist = "";
String songData = "";
String songAlbum = "";
String isChecked = "false";
public final static Parcelable.Creator<genericSongClass> CREATOR = new Parcelable.Creator<genericSongClass>() {
@Override
public genericSongClass createFromParcel(Parcel source) {
return null;
}
@Override
public genericSongClass[] newArray(int size) {
return null;
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
}
My Larger class (all parcelable related methods are at the bottom and the ArrayList and constructor using a parcel is at the top):
public class Music implements Parcelable{
private static ArrayList<genericSongClass> songs = null;
Cursor cursor;
Context context;
public Music(Context context){
this.context = context;
}
public Music(Parcel in){
songs = in.readArrayList(songs, genericSongClass.CREATOR);
}
public void BindAllSongs() {
/** Making custom drawable */
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
final String[] projection = new String[] {
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM};
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE
+ " COLLATE LOCALIZED ASC";
try {
// the uri of the table that we want to query
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// query the db
cursor = context.getContentResolver().query(uri,
projection, selection, null, sortOrder);
if (cursor != null) {
songs = new ArrayList<genericSongClass>(cursor.getCount());
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
genericSongClass GSC = new genericSongClass();
GSC.songTitle = cursor.getString(0);
GSC.songArtist = cursor.getString(1);
GSC.songData = cursor.getString(2);
GSC.songAlbum = cursor.getString(3);
songs.add(GSC);
cursor.moveToNext();
}
}
} catch (Exception ex) {
} finally {
if (cursor != null) {
cursor.close();
}
}
}
public static Object[] toArray(ArrayList<Object> list){
Object[] toReturn = new Object[list.size()];
for (int i = 0; i < list.size(); i++){
toReturn[i] = list.get(i);
}
return toReturn;
}
public ArrayList<String> getArtists(){
ArrayList<String> artists = new ArrayList<String>();
for(genericSongClass gsc: songs){
if(!artists.contains(gsc.songArtist)){
artists.add(gsc.songArtist);
}
}
Alphabetize forArtists = new Alphabetize(artists);
return forArtists.getSortedArrayList();
}
public ArrayList<String> getAlbums(String artist){
ArrayList<String> albums = new ArrayList<String>();
for(genericSongClass gsc: songs){
if(gsc.songArtist == artist){
albums.add(gsc.songAlbum);
}
}
Alphabetize forAlbums = new Alphabetize(albums);
return forAlbums.getSortedArrayList();
}
//--- Parcel ------------------------------------------------
public static final Parcelable.Creator<Music> CREATOR = new Parcelable.Creator<Music>() {
public Music createFromParcel(Parcel in) {
return new Music(in);
}
@Override
public Music[] newArray(int size) {
return null;
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(songs);
}
}
This is my first time trying to use Parcelable, so any help is welcome! Thanks in advance!
Upvotes: 1
Views: 1402
Reputation: 3133
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);
Upvotes: 4