我试图通过附加内容传递一个名为热门人物的对象的数组列表。
我在对象中实现了parcelable接口
public class PopularPeople implements Parcelable {
@SerializedName("popularity")
private double popularity;
@SerializedName("name")
private String name;
@SerializedName("profile_path")
private String profilePath;
@SerializedName("id")
private int id;
@SerializedName("adult")
private boolean adult;
public void setPopularity(double popularity){
this.popularity = popularity;
}
public double getPopularity(){
return popularity;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setProfilePath(String profilePath){
this.profilePath = profilePath;
}
public String getProfilePath(){
return profilePath;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setAdult(boolean adult){
this.adult = adult;
}
public boolean isAdult(){
return adult;
}
@Override
public String toString(){
return
"PopularPeople{" +
"popularity = '" + popularity + '\'' +
",name = '" + name + '\'' +
",profile_path = '" + profilePath + '\'' +
",id = '" + id + '\'' +
",adult = '" + adult + '\'' +
"}";
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeDouble(this.popularity);
dest.writeString(this.name);
dest.writeString(this.profilePath);
dest.writeInt(this.id);
dest.writeByte(this.adult ? (byte) 1 : (byte) 0);
}
public PopularPeople() {
}
protected PopularPeople(Parcel in) {
this.popularity = in.readDouble();
this.name = in.readString();
this.profilePath = in.readString();
this.id = in.readInt();
this.adult = in.readByte() != 0;
}
public static final Parcelable.Creator<PopularPeople> CREATOR = new Parcelable.Creator<PopularPeople>() {
@Override
public PopularPeople createFromParcel(Parcel source) {
return new PopularPeople(source);
}
@Override
public PopularPeople[] newArray(int size) {
return new PopularPeople[size];
}
};
}
现在在一个活动中,我正在创建一个新的热门人物对象,并从另一个对象Cast中创建一个PopularPeople列表
detailCastSeeAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// we need to convert CAST to POPULARPEOPLE. We only need name, profile path and ID.
ArrayList<PopularPeople> popularPeople = new ArrayList<>();
Bundle b = new Bundle();
for(Cast c : cast){
PopularPeople p = new PopularPeople();
p.setName(c.getName());
p.setId(c.getId());
p.setProfilePath(c.getProfilePath());
popularPeople.add(p);
Timber.e("Added %s",c.getName());
}
Timber.e("Length before sendin list %d",popularPeople.size());
Intent i = new Intent(MovieDetailActivity.this, PopularPeopleActivity.class);
b.putParcelableArrayList(AppConstants.CAST_LIST,popularPeople);
i.putExtras(b);
i.putExtra(AppConstants.TAG,AppConstants.MOVIE_CAST);
startActivity(i);
}
});
现在,当我在另一个类中收到此信息时,收到的数组列表仍为空,并且没有填充我从其他活动发送的数据列表。
b = getIntent().getExtras();
if (b != null){
ArrayList<PopularPeople> peopleList =
b.getParcelableArrayList(AppConstants.CAST_LIST);
tag = b != null ? b.getString(AppConstants.TAG) : null;
}else{
Toast.makeText(this, "Error loading. Please try again..", Toast.LENGTH_SHORT).show();
finish();
// return is required as we dont want to execute the code below and just want the activity to close.
return;
}
在我收到意图的第二个活动中,如果我注销了受欢迎的人的大小,它会给出0.如何解决这个问题?
答案 0 :(得分:0)
您可能需要考虑将Arraylist保存到SharedPreferences并通过额外的intent传递密钥。
我觉得最容易使用Gson和json将自定义对象的Arraylist写入SharedPreferences。 这是一个关于如何做到这一点的好教程:https://youtu.be/jcliHGR3CHo
但如果您想使用parcelable,我建议您观看本教程:https://youtu.be/WBbsvqSu0is
我希望这可以帮到你!
答案 1 :(得分:0)
您可以使用&#34; serializable&#34;而不是&#34; parceable&#34 ;.
尝试这种方式可以帮助您
首先要制作
Class Venue实现Serializable
public class Venue implements Serializable { /** * */
private static final long serialVersionUID = 1L; }
在您的FirstActivity中这样做
ArrayList<Venue> VenueArrayList = new ArrayList<Venue>();
Intent intent = new Intent(this,secondActivity.class);
intent.putExtra("VenueArrayList", VenueArrayList);
在您的SecondActivity中这样做
ArrayList<Venue> VenueArrayList; VenueArrayList = (ArrayList<Venue>)
getIntent().getSerializableExtra( "VenueArrayList");