我通过意图将电影对象的ArrayList从MainActivity
传递到DetailActivity
。
intent.putExtra(KEY_mFavoriteMovies, mFavoriteMovies);
startActivity(intent);
在其他活动中,我添加了其他电影对象。
mFavoriteMovies(movie);
但是,mFavoriteMovies
中的MainActivity
似乎不包含其他影片对象。
预计会出现这种情况吗?
答案 0 :(得分:0)
方法取决于什么是电影列表...
putStringArrayListExtra
和getStringArrayListExtra
。putParcelableArrayListExtra
和
getParcelableArrayList
。如果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
开始。这将为您提供一个选项,使用DetailsActivity
到setResult
到MainActivity
回调,从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();
}