Skip to content

Instantly share code, notes, and snippets.

@devAseemSharma
Last active March 22, 2018 15:22
Show Gist options
  • Save devAseemSharma/aaa321d17e8171352aec7dd52538a8b8 to your computer and use it in GitHub Desktop.
Save devAseemSharma/aaa321d17e8171352aec7dd52538a8b8 to your computer and use it in GitHub Desktop.
Artist Model Class with Parcelable
public class Artist implements Parcelable
{
private String artistName;
private String albumName;
private String imgAlbum;
private String artistTitle;
public final static Parcelable.Creator<Artist> CREATOR = new Creator<Artist>() {
@SuppressWarnings({
"unchecked"
})
public Artist createFromParcel(Parcel in) {
return new Artist(in);
}
public Artist[] newArray(int size) {
return (new Artist[size]);
}
}
;
protected Artist(Parcel in) {
this.artistName = ((String) in.readValue((String.class.getClassLoader())));
this.albumName = ((String) in.readValue((String.class.getClassLoader())));
this.imgAlbum = ((String) in.readValue((String.class.getClassLoader())));
this.artistTitle = ((String) in.readValue((String.class.getClassLoader())));
}
public Artist() {
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
public String getAlbumName() {
return albumName;
}
public void setAlbumName(String albumName) {
this.albumName = albumName;
}
public String getImgAlbum() {
return imgAlbum;
}
public void setImgAlbum(String imgAlbum) {
this.imgAlbum = imgAlbum;
}
public String getArtistTitle() {
return artistTitle;
}
public void setArtistTitle(String artistTitle) {
this.artistTitle = artistTitle;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(artistName);
dest.writeValue(albumName);
dest.writeValue(imgAlbum);
dest.writeValue(artistTitle);
}
public int describeContents() {
return 0;
}
}
Changes you need to do in your MainActivity and ShowTracksActivity classes:
In MainActivity.class :
Artist artist = artists.get(position);
intent.putExtra ("key", artist);
In ShowTracksActivity.class :
getIntent().getParcelableExtra("key");
@Payfu
Copy link

Payfu commented Mar 22, 2018

How do you implement @SerializedName & @expose ? Cannot resolve symbol 'SerializedName'

Anyway, I want to thank you for your help !

@devAseemSharma
Copy link
Author

devAseemSharma commented Mar 22, 2018

@serailzdename & @expose are part of the Gson library, if you want you can remove them it will not be problem(edited the gist for your convenience), Gson is used for parsing JSON to POJO by the way.

@devAseemSharma
Copy link
Author

Please accept my answer if it helped you. Thanks

@Payfu
Copy link

Payfu commented Mar 22, 2018

Before getting your code, I did this (it seems similar):

`public class Artist implements Parcelable {

private String mAlbumTitle, mAlbumImage, mArtistName;
private String [] mTracks;

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel out, int flags) {
    out.writeString(mAlbumTitle);
    out.writeString(mAlbumImage);
    out.writeString(mArtistName);
    out.writeStringArray(mTracks);
}

public static final Parcelable.Creator<Artist> CREATOR = new Parcelable.Creator<Artist>() {
    public Artist createFromParcel(Parcel in) {
        return new Artist(in);
    }

    public Artist[] newArray(int size) {
        return new Artist[size];
    }
};

private Artist(Parcel in) {
    mAlbumTitle = in.readString();
    mAlbumImage = in.readString();
    mArtistName = in.readString();
    mTracks = in.createStringArray();
}

public Artist(String artistName, String albumTitle, String albumImage, String [] tracks){
    mAlbumTitle = albumTitle;
    mAlbumImage = albumImage;
    mArtistName = artistName;
    mTracks = tracks;
}

public String getAlbumTitle(){
    return mAlbumTitle;
}

public String getAlbumImage(){
    return mAlbumImage;
}

public String getArtistName(){
    return mArtistName;
}

public String[] getTracks(){
    return mTracks;
}

}`

With your proposition of getIntent().getParcelableExtra("key"); it returns to me this : com.example.android.app.Artist@e54b310
But I don't kown how get data.

getIntent().getParcelableExtra("key").get(index) does not work...

@Payfu
Copy link

Payfu commented Mar 22, 2018

YES !

I did this and it works !

Intent intent = getIntent();
Artist value = (Artist) intent.getParcelableExtra("key");

Thank you so much for your help !

@devAseemSharma
Copy link
Author

devAseemSharma commented Mar 22, 2018

You're welcome. And you figured out the problem great work for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment