0

I'm trying to save my data using SharedPreferences by converting my ArrayList into JSON. Whenever I try to retrieve data and convert it back I get cannot cast ArrayList into Playlist error. Any ideas what's might be wrong here?

My Playlist class:

public class Playlist extends ArrayList<Parcelable> implements Parcelable{
private ArrayList<Song> playlistSongs;
private String name;

public Playlist(String name) {
    this.name = name;
    this.playlistSongs = new ArrayList<>();
}

protected Playlist(Parcel in) {
    playlistSongs = in.createTypedArrayList(Song.CREATOR);
    name = in.readString();
}

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

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

public ArrayList<Song> getPlaylistSongs() {
    return playlistSongs;
}

public String getName() {
    return name;
}

@Override
public String toString() {
    return name;
}

public int getCount(){
    return playlistSongs.size();
}

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

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeTypedList(playlistSongs);
    parcel.writeString(name);
}

@Override
public Stream<Parcelable> stream() {
    return null;
}

@Override
public Stream<Parcelable> parallelStream() {
    return null;
}

}

My convert to JSON method I call after creating all of the playlists in activity:

public boolean writeJSON(ArrayList<Playlist> playlists) {

    SharedPreferences mSettings = getSharedPreferences("Playlists", Context.MODE_PRIVATE);
    SharedPreferences.Editor mEditor = mSettings.edit();

    Type type = new TypeToken<ArrayList<Playlist>>() {
    }.getType();
    try {
        String writeValue = mGson.toJson(playlists, type);
        mEditor.putString("Playlists", writeValue);
        mEditor.commit();
        return true;
    } catch (Exception e) {
        return false;
    }
}

And my read JSON method I call after user relaunch application:

    public ArrayList<Playlist> readJSON() {

    SharedPreferences mSettings = getSharedPreferences("Playlists", Context.MODE_PRIVATE);
    Type collectionType = new TypeToken<ArrayList<Playlist>>() {
    }.getType();
    String loadValue = mSettings.getString("Playlists", null);
    ArrayList<Playlist> temp = mGson.fromJson(loadValue, collectionType);
    return temp;
}

2 Answers 2

1

I believe the issue is that the Playlist class extends ArrayList. GSON is trying to serialize your Playlist as a list of Parcelable. Judging by your class, it doesn't look like Playlist truly needs to extend ArrayList. I tried running your example (I had to remove a lot in order to get down to the bare minimum) and it was serializing each Playlist as an empty list (the string "[]"), regardless of what was in playlistSongs. I removed the superclass and it started working as expected.

Sign up to request clarification or add additional context in comments.

2 Comments

I haven't even noticed I extended my Playlist class. Thank you it's working!
Glad I could help!
1

I think that the problem is this extends ArrayList<Parcelable>. Try to make it extends ArrayList<Song> and also you do not need to hold a reference for the Arraylist (private ArrayList<Song> playlistSongs;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.