Last active
March 22, 2018 15:22
-
-
Save devAseemSharma/aaa321d17e8171352aec7dd52538a8b8 to your computer and use it in GitHub Desktop.
Artist Model Class with Parcelable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
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...
YES !
I did this and it works !
Intent intent = getIntent();
Artist value = (Artist) intent.getParcelableExtra("key");
Thank you so much for your help !
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
Please accept my answer if it helped you. Thanks