我可以成功地将一个arraylist从活动A传递给活动B.如下所示:
在活动A :
Intent intent = new Intent(Activity_A.this, Activity_B.class);
intent.putParcelableArrayListExtra("albums", (ArrayList<? extends Parcelable>) albums);
startActivity(intent);
模型相册的实施方式如下:
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);
}
}
如图所示,该专辑包含歌曲的arraylist和另一个数据
以下是歌曲模型的实现:
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);
}
}
}
并在活动B处传递数据
Bundle bundle = getIntent().getExtras();
ArrayList<Album> albums = bundle.getParcelableArrayList("albums");
这里的问题是数据成功传递从活动A到活动B 但是虽然我在活动A中检查了每张专辑的列表,但每张专辑的歌曲列表为空,大小为零歌曲
答案 0 :(得分:3)
您的歌曲列表为空,因为您没有在SuperTabLayout
课程的writeToParcel(Parcel, int)
方法中将其保存到Parcel。
您的Album
方法应如下所示:
writeToParcel(Parcel, int)
@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);
}
就像这样:
Album(Parcel)
答案 1 :(得分:0)
您可以像这样传递ArrayList,
在 ActivityA
中 Intent intent = new Intent(Activity_A.this, Activity_B.class);
intent.putParcelableArrayListExtra("albums", albums);
startActivity(intent);
在 ActivityB
中 Bundle bundle = getIntent().getExtras();
ArrayList<Album> albums=(ArrayList<Album>) bundle.getSerializable("albums");