java - 如何通过Intent发送Object的ArrayList?

标签 java android android-intent arraylist

我正在尝试通过 extras 传递一个名为“popular people”的对象的数组列表。

我已经在对象中实现了parcelable接口(interface)

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];
    }
};
}

现在,在一项 Activity 中,我正在创建一个新的热门人物对象,并从另一个对象 Cast 创建一个热门人物列表

        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);
        }
    });

现在,当我在另一个类中收到此消息时,收到的数组列表仍然为空,并且未填充我从其他 Activity 发送的数据列表。

           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;
    }

在我收到 Intent 的第二个 Activity 中,如果我注销受欢迎的人数,它会给出 0。我该如何解决这个问题?

最佳答案

您可能需要考虑将 Arraylist 保存到 SharedPreferences 并通过 Intent extra 将 key 传递给它。

我发现使用 Gson 和 json 将自定义对象的 Arraylist 写入 SharedPreferences 是最简单的。 这是关于如何执行此操作的一个很好的教程:https://youtu.be/jcliHGR3CHo

但是如果您想使用 Parcelable,我建议您观看本教程:https://youtu.be/WBbsvqSu0is

希望这对你有帮助!

关于java - 如何通过Intent发送Object的ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47463237/

相关文章:

android - 为什么我必须分离 <intent-filter> 才能在 google play 上显示 "open"按钮?

Android - 如何将徽章计数添加到应用程序图标?

java - 导致构建错误的 Android Intent setType 和 addCategory 方法

android - 如何使用未在Phone-Android中安装的应用程序的程序包ID获取应用程序图标

java - Sonar 占用太多空间和时间

java - 反射 Java 帮助

java - 具有前瞻功能的正则表达式性能/速度较差

android - Android Gradle 插件仅支持 Crashlytics Gradle 插件版本 1.28.0 及更高版本

Android 锁屏通知无法双击打开浏览器

Java StreamTokenizer 将不带空格的数字和字符作为单独的标记