i could to pass an arraylist from activity A to Activity B Successfully. AS Shown below :
At activity A :
Intent intent = new Intent(Activity_A.this, Activity_B.class);
intent.putParcelableArrayListExtra("albums", (ArrayList<? extends Parcelable>) albums);
startActivity(intent);
The Model Album is implemented as below:
public class Album implements Parcelable
{
public ArrayList<Song> Songs = new ArrayList<Song>();
public int id;
public String album_name;
public String pic;
public static final Creator<Album> CREATOR = new Creator<Album>()
{
@Override
public Album createFromParcel(Parcel in)
{
return new Album(in);
}
@Override
public Album[] newArray(int size)
{
return new Album[size];
}
};
@Override
public int describeContents()
{
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i)
{
parcel.writeInt(id);
parcel.writeString(album_name);
parcel.writeString(pic);
}
}
As Shown the album contain of arraylist of songs and another data
Here is the implementation of Song Model:
public class Song implements Parcelable
{
public int id;
public String file_name;
public String file_url;
public String pic_url;
public int Album_id;
public static final Creator<Song> CREATOR = new Creator<Song>()
{
@Override
public Song createFromParcel(Parcel in)
{
return new Song(in);
}
@Override
public Song[] newArray(int size)
{
return new Song[size];
}
};
@Override
public int describeContents()
{
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i)
{
parcel.writeInt(id);
parcel.writeString(file_name);
parcel.writeString(file_url);
parcel.writeString(pic_url);
parcel.writeInt(Album_id);
}
}
}
And to get data passed at At activity B :
Bundle bundle = getIntent().getExtras();
ArrayList<Album> albums = bundle.getParcelableArrayList("albums");
The problem here is that Data is passed successfully From Activity A to Activity B But The list of songs is null with size zero for every album although i checked in activity A that every album has list of songs
Your songs list is empty because you didn't save it to Parcel in writeToParcel(Parcel, int)
method in Album
class.
Your writeToParcel(Parcel, int)
method should look like this:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(this.Songs);
dest.writeInt(this.id);
dest.writeString(this.album_name);
dest.writeString(this.pic);
}
And Album(Parcel)
like this:
protected Album(Parcel in) {
this.Songs = in.createTypedArrayList(Song.CREATOR);
this.id = in.readInt();
this.album_name = in.readString();
this.pic = in.readString();
}