我想要将List<Movie>
从一个活动传递到另一个活动:
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "CLICKED", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), IndividualMovieActivity.class);
i.putExtra("Movie", mMovieResultsList.get(position));
startActivity(i);
Movie.Model:
public class Movie implements Parcelable {
private int vote_count;
private int id;
private boolean video;
private String title;
private float popularity;
private String poster_path;
private String original_language;
private String original_title;
private ArrayList<String> genre_ids;
private String backdrop_path;
private boolean adult;
private String overview;
private Date release_date;
public Movie(){
}
public Movie(int vote_count, int id, boolean video, String title, float popularity, String poster_path, String original_language, String original_title, ArrayList<String> genre_ids, String backdrop_path, boolean adult, String overview, Date release_date) {
this.vote_count = vote_count;
this.id = id;
this.video = video;
this.title = title;
this.popularity = popularity;
this.poster_path = poster_path;
this.original_language = original_language;
this.original_title = original_title;
this.genre_ids = genre_ids;
this.backdrop_path = backdrop_path;
this.adult = adult;
this.overview = overview;
this.release_date = release_date;
}
protected Movie(Parcel in) {
vote_count = in.readInt();
id = in.readInt();
video = in.readByte() != 0;
title = in.readString();
popularity = in.readFloat();
poster_path = in.readString();
original_language = in.readString();
original_title = in.readString();
genre_ids = in.createStringArrayList();
backdrop_path = in.readString();
adult = in.readByte() != 0;
overview = in.readString();
}
public static final Creator<Movie> CREATOR = new Creator<Movie>() {
@Override
public Movie createFromParcel(Parcel in) {
return new Movie(in);
}
@Override
public Movie[] newArray(int size) {
return new Movie[size];
}
};
public void setVote_count(int vote_count) {
this.vote_count = vote_count;
}
public void setId(int id) {
this.id = id;
}
public void setVideo(boolean video) {
this.video = video;
}
public void setTitle(String title) {
this.title = title;
}
public void setPopularity(float popularity) {
this.popularity = popularity;
}
public void setPoster_path(String poster_path) {
this.poster_path = poster_path;
}
public void setOriginal_language(String original_language) {
this.original_language = original_language;
}
public void setOriginal_title(String original_title) {
this.original_title = original_title;
}
public void setGenre_ids(ArrayList<String> genre_ids) {
this.genre_ids = genre_ids;
}
public void setBackdrop_path(String backdrop_path) {
this.backdrop_path = backdrop_path;
}
public void setAdult(boolean adult) {
this.adult = adult;
}
public void setOverview(String overview) {
this.overview = overview;
}
public void setRelease_date(Date release_date) {
this.release_date = release_date;
}
public int getVote_count() {
return vote_count;
}
public int getId() {
return id;
}
public boolean isVideo() {
return video;
}
public String getTitle() {
return title;
}
public float getPopularity() {
return popularity;
}
public String getPoster_path() {
return poster_path;
}
public String getOriginal_language() {
return original_language;
}
public String getOriginal_title() {
return original_title;
}
public ArrayList<String> getGenre_ids() {
return genre_ids;
}
public String getBackdrop_path() {
return backdrop_path;
}
public boolean isAdult() {
return adult;
}
public String getOverview() {
return overview;
}
public Date getRelease_date() {
return release_date;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(vote_count);
parcel.writeInt(id);
parcel.writeBooleanArray(new boolean[]{video});
parcel.writeString(title);
parcel.writeFloat(popularity);
parcel.writeString(poster_path);
parcel.writeString(original_language);
parcel.writeString(original_title);
parcel.writeStringList(genre_ids);
parcel.writeString(backdrop_path);
parcel.writeBooleanArray(new boolean[]{adult});
parcel.writeString(overview);
parcel.writeString(String.valueOf(release_date));
}
}
当我尝试获取数据并在TextView
中显示时,没有显示任何内容表明我可能没有正确传递数据:
public class IndividualMovieActivity extends AppCompatActivity {
private TextView testText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_individual_movie);
Intent intent = getIntent();
Movie movie = intent.getParcelableExtra("Movie");
String title = movie.getOriginal_title();
testText = findViewById(R.id.movie_name);
//no text being displayed
testText.setText(title);
}
}
更新 完整的源代码可以在这里找到: https://github.com/troy21688/PopularMovies
答案 0 :(得分:2)
Pracelable实现中存在错误,从Parcel读取数据时会导致数据损坏。
您应该使用:
parcel.writeByte(theBoolean ? (byte) 1 : (byte) 0); // replace `theBoolean` with the real variable name
保存布尔值(不是writeBooleanArray
)。在您的情况下,video
和adult
。
答案 1 :(得分:1)
要获取Parcelable对象,您需要使用
getIntent().getExtras().getParcelable(mKey);
如果您遇到问题Parcelable类请尝试以下(不添加getter和setter)
public class Movie implements Parcelable {
private int vote_count;
private int id;
private boolean video;
private String title;
private float popularity;
private String poster_path;
private String original_language;
private String original_title;
private ArrayList<String> genre_ids;
private String backdrop_path;
private boolean adult;
private String overview;
private Date release_date;
public Movie() {
}
protected Movie(Parcel in) {
this.vote_count = in.readInt();
this.id = in.readInt();
this.video = in.readByte() != 0;
this.title = in.readString();
this.popularity = in.readFloat();
this.poster_path = in.readString();
this.original_language = in.readString();
this.original_title = in.readString();
this.genre_ids = in.createStringArrayList();
this.backdrop_path = in.readString();
this.adult = in.readByte() != 0;
this.overview = in.readString();
long tmpRelease_date = in.readLong();
this.release_date = tmpRelease_date == -1 ? null : new Date(tmpRelease_date);
}
public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() {
@Override
public Movie createFromParcel(Parcel source) {
return new Movie(source);
}
@Override
public Movie[] newArray(int size) {
return new Movie[size];
}
};
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.vote_count);
dest.writeInt(this.id);
dest.writeByte(this.video ? (byte) 1 : (byte) 0);
dest.writeString(this.title);
dest.writeFloat(this.popularity);
dest.writeString(this.poster_path);
dest.writeString(this.original_language);
dest.writeString(this.original_title);
dest.writeStringList(this.genre_ids);
dest.writeString(this.backdrop_path);
dest.writeByte(this.adult ? (byte) 1 : (byte) 0);
dest.writeString(this.overview);
dest.writeLong(this.release_date != null ? this.release_date.getTime() : -1);
}
@Override
public int describeContents() {
return 0;
}
}
答案 2 :(得分:1)
而不是getIntent()。 尝试getExtra()。getParcelable(“Movie”)
答案 3 :(得分:1)
您可以使用此模型。
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.Date;
public class Movie implements Parcelable {
private int vote_count;
private int id;
private boolean video;
private String title;
private float popularity;
private String poster_path;
private String original_language;
private String original_title;
private ArrayList<String> genre_ids;
private String backdrop_path;
private boolean adult;
private String overview;
private Date release_date;
public Movie() {
}
public Movie(int vote_count, int id, boolean video, String title, float popularity, String poster_path, String original_language, String original_title, ArrayList<String> genre_ids, String backdrop_path, boolean adult, String overview, Date release_date) {
this.vote_count = vote_count;
this.id = id;
this.video = video;
this.title = title;
this.popularity = popularity;
this.poster_path = poster_path;
this.original_language = original_language;
this.original_title = original_title;
this.genre_ids = genre_ids;
this.backdrop_path = backdrop_path;
this.adult = adult;
this.overview = overview;
this.release_date = release_date;
}
public int getVote_count() {
return vote_count;
}
public void setVote_count(int vote_count) {
this.vote_count = vote_count;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isVideo() {
return video;
}
public void setVideo(boolean video) {
this.video = video;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public float getPopularity() {
return popularity;
}
public void setPopularity(float popularity) {
this.popularity = popularity;
}
public String getPoster_path() {
return poster_path;
}
public void setPoster_path(String poster_path) {
this.poster_path = poster_path;
}
public String getOriginal_language() {
return original_language;
}
public void setOriginal_language(String original_language) {
this.original_language = original_language;
}
public String getOriginal_title() {
return original_title;
}
public void setOriginal_title(String original_title) {
this.original_title = original_title;
}
public ArrayList<String> getGenre_ids() {
return genre_ids;
}
public void setGenre_ids(ArrayList<String> genre_ids) {
this.genre_ids = genre_ids;
}
public String getBackdrop_path() {
return backdrop_path;
}
public void setBackdrop_path(String backdrop_path) {
this.backdrop_path = backdrop_path;
}
public boolean isAdult() {
return adult;
}
public void setAdult(boolean adult) {
this.adult = adult;
}
public String getOverview() {
return overview;
}
public void setOverview(String overview) {
this.overview = overview;
}
public Date getRelease_date() {
return release_date;
}
public void setRelease_date(Date release_date) {
this.release_date = release_date;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.vote_count);
dest.writeInt(this.id);
dest.writeByte(this.video ? (byte) 1 : (byte) 0);
dest.writeString(this.title);
dest.writeFloat(this.popularity);
dest.writeString(this.poster_path);
dest.writeString(this.original_language);
dest.writeString(this.original_title);
dest.writeStringList(this.genre_ids);
dest.writeString(this.backdrop_path);
dest.writeByte(this.adult ? (byte) 1 : (byte) 0);
dest.writeString(this.overview);
dest.writeLong(this.release_date != null ? this.release_date.getTime() : -1);
}
protected Movie(Parcel in) {
this.vote_count = in.readInt();
this.id = in.readInt();
this.video = in.readByte() != 0;
this.title = in.readString();
this.popularity = in.readFloat();
this.poster_path = in.readString();
this.original_language = in.readString();
this.original_title = in.readString();
this.genre_ids = in.createStringArrayList();
this.backdrop_path = in.readString();
this.adult = in.readByte() != 0;
this.overview = in.readString();
long tmpRelease_date = in.readLong();
this.release_date = tmpRelease_date == -1 ? null : new Date(tmpRelease_date);
}
public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() {
@Override
public Movie createFromParcel(Parcel source) {
return new Movie(source);
}
@Override
public Movie[] newArray(int size) {
return new Movie[size];
}
};
}
答案 4 :(得分:1)
你错过了代码中的表达
protected Movie(Parcel in) {
vote_count = in.readInt();
id = in.readInt();
video = in.readByte() != 0;
title = in.readString();
popularity = in.readFloat();
poster_path = in.readString();
original_language = in.readString();
original_title = in.readString();
genre_ids = in.createStringArrayList();
backdrop_path = in.readString();
adult = in.readByte() != 0;
overview = in.readString();
// here you miss the realease date field. you can use below
long tmpDate = in.readLong();
release_date = tmpDate == -1 ? null : new Date(tmpDate);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
...
// add corresponding write to parcel
dest.writeLong(release_date != null ? release_date.getTime() : -1);
}