无法通过intent extras传递对象数组

时间:2017-07-25 23:00:06

标签: java android android-intent android-activity parcelable

我通过意图将电影对象的ArrayList从MainActivity传递到DetailActivity

intent.putExtra(KEY_mFavoriteMovies, mFavoriteMovies);
startActivity(intent);

在其他活动中,我添加了其他电影对象。

mFavoriteMovies(movie);

但是,mFavoriteMovies中的MainActivity似乎不包含其他影片对象。

预计会出现这种情况吗?

3 个答案:

答案 0 :(得分:0)

方法取决于什么是电影列表...

  • 如果您的电影列表是字符串的简单ArrayList,则使用 putStringArrayListExtragetStringArrayListExtra
  • 如果它们是Object(单独的类),那么你应该实现 Parcelable并使用putParcelableArrayListExtragetParcelableArrayList

如果MovieName是对象/类,则实现Parcelable

import android.os.Parcel;
import android.os.Parcelable;

public class MovieName implements Parcelable {

    private String name;

    private String year;

    public MovieName(String name, String year) {
        this.name = name;
        this.year = year;
    }

    public MovieName(Parcel in) {
        name = in.readString();
        year = in.readString();
    }

    // Creator
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public MovieName createFromParcel(Parcel in) {
            return new MovieName(in);
        }

        public MovieName[] newArray(int size) {
            return new MovieName[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(name);
        parcel.writeString(year);
    }

    @Override
    public String toString() {
        return "MovieName=[name=" + name + ";year=" + year + "]";
    }
}
当您即将MainActivity转到startActivity

时,

DetailActivity

Intent intent = new Intent(view.getContext(), DetailActivity.class);

ArrayList<String> movieNames = new ArrayList<String>();
movieNames.add("Star Wars");
movieNames.add("Transporter");
movieNames.add("Need for Speed");
intent.putStringArrayListExtra("MOVIE_NAMES", movieNames);
Log.d("Movie Names", "Writing into intent ... " + movieNames.toString());

ArrayList<MovieName> movieNameList = new ArrayList<MovieName>();
movieNameList.add(new MovieName("Star Wars", "1900"));
movieNameList.add(new MovieName("Transporter", "2000"));
intent.putParcelableArrayListExtra("movie", movieNameList);
Log.d("MovieName Parcelable", "Writing into intent ... " + movieNameList.toString());

startActivity(intent);

内部DetailActivity's onCreate

ArrayList<String> movieNames = getIntent().getStringArrayListExtra("MOVIE_NAMES");
Log.d("Movie Names", "Reading from intent ... " + movieNames.toString());

ArrayList<MovieName> movieNameList = getIntent().getExtras().getParcelableArrayList("movie");
Log.d("MovieName Parcelable", "Reading from intent ... " + movieNameList.toString());

示例运行

07-25 16:32:53.108 5264-5264/? D/Movie Names: Writing into intent ... [Star Wars, Transporter, Need for Speed]
07-25 16:32:53.111 5264-5264/? D/MovieName Parcelable: Writing into intent ... [MovieName=[name=Star Wars;year=1900], MovieName=[name=Transporter;year=2000]]

07-25 16:32:53.144 5264-5264/? D/Movie Names: Reading from intent ... [Star Wars, Transporter, Need for Speed]
07-25 16:32:53.145 5264-5264/? D/MovieName Parcelable: Reading from intent ... [MovieName=[name=Star Wars;year=1900], MovieName=[name=Transporter;year=2000]]

答案 1 :(得分:0)

除了@JRG答案,要获得MainActivity中的更新列表,您需要将DetailActivity作为startActivityForResult开始。这将为您提供一个选项,使用DetailsActivitysetResultMainActivity回调,从onActivityResult返回更新的数组。

另一种方法是通过将View部分(活动)与模型(喜欢的电影)分开来改变应用程序的体系结构来修复它。

答案 2 :(得分:0)

这是预期的行为,因为当您在DetailActivity修改数组列表时,您正在处理电影列表的其他实例。

您可以使用DetailActivity启动startActivityForResult,然后将更新后的列表发送回MainActivity

,以获取对列表所做的更改

您的MainActivity

会有类似的内容
ArrayList<String> mMovies;
int DETAIL_REQUEST_CODE = 123;
String KEY_FAVORITE_MOVIES = "key-movies";

private void startDetailActivity() {
    Intent intent = new Intent(this, DetailActivity.class);
    intent.putStringArrayListExtra(KEY_FAVORITE_MOVIES, mMovies);
    startActivityForResult(intent, DETAIL_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == DETAIL_REQUEST_CODE && resultCode == RESULT_OK) {
        mMovies = data.getStringArrayListExtra(KEY_FAVORITE_MOVIES);
    }
}

您必须使用以下内容发送DetailActivity的更新列表:

public void finishDetail() {
    Intent resultIntent = new Intent();
    resultIntent.putStringArrayListExtra(KEY_FAVORITE_MOVIES, mMovies);
    setResult(Activity.RESULT_OK, resultIntent);
    finish();
}