我有一个已实现parcelable的类,我想将它传递给另一个活动。
这就是我的理由
public class Result implements Parcelable {
public static final String TMDB_IMAGE_PATH = "http://image.tmdb.org/t/p/w500";
@SerializedName("vote_count")
@Expose
private Integer voteCount;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("video")
@Expose
private Boolean video;
@SerializedName("vote_average")
@Expose
private Double voteAverage;
@SerializedName("title")
@Expose
private String title;
@SerializedName("popularity")
@Expose
private Double popularity;
@SerializedName("poster_path")
@Expose
private String posterPath;
@SerializedName("original_language")
@Expose
private String originalLanguage;
@SerializedName("original_title")
@Expose
private String originalTitle;
@SerializedName("genre_ids")
@Expose
private JsonArray genreIds = null;
@SerializedName("backdrop_path")
@Expose
private String backdropPath;
@SerializedName("adult")
@Expose
private Boolean adult;
@SerializedName("overview")
@Expose
private String overview;
@SerializedName("release_date")
@Expose
private String releaseDate;
public Result(Integer voteCount, Integer id, Boolean video,Double voteAverage ,
String title ,Double popularity,String posterPath, String originalLanguage,
String originalTitle, JsonArray genreIds,String backdropPath,Boolean adult, String overview,String releaseDate){
this.voteCount = voteCount;
this.id = id;
this.video = video;
this.voteAverage = voteAverage;
this.title = title;
this.popularity = popularity;
this.posterPath = posterPath;
this.originalLanguage = originalLanguage;
this.originalTitle = originalTitle;
this.genreIds = genreIds;
this.backdropPath = backdropPath;
this.adult = adult;
this.overview = overview;
this.releaseDate = releaseDate;
}
private Result(Parcel source) {
this.voteCount = source.readInt();
this.id = source.readInt();
this.video = source.readByte() != 0;
this.voteAverage = source.readDouble();
this.title = source.readString();
this.popularity = source.readDouble();
this.posterPath = source.readString();
this.originalLanguage = source.readString();
this.originalTitle = source.readString();
this.backdropPath = source.readString();
this.adult = source.readByte() != 0;
this.overview = source.readString();
this.releaseDate = source.readString();
}
public Integer getVoteCount() {
return voteCount;
}
public void setVoteCount(Integer voteCount) {
this.voteCount = voteCount;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Boolean getVideo() {
return video;
}
public void setVideo(Boolean video) {
this.video = video;
}
public Double getVoteAverage() {
return voteAverage;
}
public void setVoteAverage(Double voteAverage) {
this.voteAverage = voteAverage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getPopularity() {
return popularity;
}
public void setPopularity(Double popularity) {
this.popularity = popularity;
}
public String getPosterPath() {
return TMDB_IMAGE_PATH + posterPath;
}
public void setPosterPath(String posterPath) {
this.posterPath = posterPath;
}
public String getOriginalLanguage() {
return originalLanguage;
}
public void setOriginalLanguage(String originalLanguage) {
this.originalLanguage = originalLanguage;
}
public String getOriginalTitle() {
return originalTitle;
}
public void setOriginalTitle(String originalTitle) {
this.originalTitle = originalTitle;
}
public JsonArray getGenreIds() {
return genreIds;
}
public void setGenreIds(JsonArray genreIds) {
this.genreIds = genreIds;
}
public String getBackdropPath() {
return TMDB_IMAGE_PATH +backdropPath;
}
public void setBackdropPath(String backdropPath) {
this.backdropPath = backdropPath;
}
public Boolean getAdult() {
return adult;
}
public void setAdult(Boolean adult) {
this.adult = adult;
}
public String getOverview() {
return overview;
}
public void setOverview(String overview) {
this.overview = overview;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
public static final Parcelable.Creator<Result> CREATOR = new Parcelable.Creator<Result>() {
public Result createFromParcel(Parcel source) {
Result movie = new Result(source);
return movie;
}
public Result[] newArray(int size) {
return new Result[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int i) {
dest.writeString(posterPath);
dest.writeByte((byte) (adult ? 1 : 0));
dest.writeString(overview);
dest.writeString(releaseDate);
dest.writeInt(id);
dest.writeString(originalTitle);
dest.writeString(originalLanguage);
dest.writeString(title);
dest.writeString(backdropPath);
dest.writeDouble(popularity);
dest.writeInt(voteCount);
dest.writeByte((byte) (video ? 1 : 0));
dest.writeDouble(voteAverage);
}
}
这是我从RecycerView适配器发送Parcelable类的方法
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
private List<Result> movies;
private int rowLayout;
private Context context;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView movieTitle;
TextView data;
TextView movieDescription;
TextView rating;
ImageView pic;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
movieTitle = (TextView) itemView.findViewById(R.id.title);
data = (TextView) itemView.findViewById(R.id.subtitle);
rating = (TextView) itemView.findViewById(R.id.rating);
pic = (ImageView) itemView.findViewById(R.id.image_view_pic);
}
@Override
public void onClick(View view) {
int itemPosition = getAdapterPosition();
Result movie = movies.get(itemPosition);
Intent intent = new Intent(view.getContext(), MovieDetailActivity.class);
intent.putExtra("movieDetails",movie);
view.getContext().startActivity(intent);
}
}
public MovieAdapter(List<Result> movies, Context context){
this.movies = movies;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_movie, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.movieTitle.setText(movies.get(position).getTitle());
holder.data.setText(movies.get(position).getReleaseDate());
holder.rating.setText(movies.get(position).getVoteAverage().toString());
String PictureString = movies.get(position).getPosterPath();
Picasso.with(context)
.load(PictureString)
.placeholder(R.color.colorAccent)
.into(holder.pic);
}
@Override
public int getItemCount() {
return movies.size();
}
}
这是我收到它的地方
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
Intent intent = getIntent();
Result films = intent.getParcelableExtra("movieDetails");
}
所以这段代码的问题是我在接收结束时得到一个空值,但我的Recycler视图正在显示数据。 有人能指出我的问题吗?
答案 0 :(得分:1)
写入数据和阅读数据的顺序应该相同。在writeToParcel()
时,您首先要写posterPath
@Override
public void writeToParcel(Parcel dest, int i) {
dest.writeString(posterPath);
....
}
然后从Parcel
阅读时,您应该先阅读它。如下所示:
private Result(Parcel source) {
this.posterPath = source.readString();
...
}
等等。
答案 1 :(得分:0)
//我们的属性类型名称
public class Property implements Parcelable {
...
}
//在意图
中发送我们的parcelable模型类Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("Property", property);
startActivity(intent);
//收集我们的意图
Intent intent = getIntent();
Property property = intent.getParcelableExtra("Property");