我已经审核过类似的问题,但我没有达到要求的结果
我从电影数据库服务器中提取数据,我正在使用Jackson解析该数据,而我正在创建这个对象,我需要在其中实现parcelable,就像我要添加的活动一样onSaveInstanceState方法中包含这些对象的列表。
import android.os.Parcel;
import android.os.Parcelable;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
public class Movie implements Parcelable{
@JsonProperty("adult")
private Boolean adult;
@JsonProperty("backdrop_path")
private String backdropPath;
@JsonProperty("genre_ids")
private List<Integer> genreIds = new ArrayList<Integer>();
@JsonProperty("id")
private Integer id;
@JsonProperty("original_language")
private String originalLanguage;
@JsonProperty("original_title")
private String originalTitle;
@JsonProperty("overview")
private String overview;
@JsonProperty("release_date")
private String releaseDate;
@JsonProperty("poster_path")
private String posterPath;
@JsonProperty("popularity")
private Double popularity;
@JsonProperty("title")
private String title;
@JsonProperty("video")
private Boolean video;
@JsonProperty("vote_average")
private Double voteAverage;
@JsonProperty("vote_count")
private Integer voteCount;
public Movie(){}
private Movie(Parcel in){
adult = (in.readInt() == 0) ? false : true;
backdropPath = in.readString();
genreIds = in.readArrayList(List.class.getClassLoader());
id = in.readInt();
originalLanguage = in.readString();
originalTitle = in.readString();
overview = in.readString();
releaseDate = in.readString();
posterPath = in.readString();
popularity = in.readDouble();
title = in.readString();
video = (in.readInt() == 0) ? false : true;
voteAverage = in.readDouble();
voteCount = in.readInt();
}
/**
*
* @return
* The adult
*/
@JsonProperty("adult")
public Boolean getAdult() {return adult;}
/**
*
* @param adult
* The adult
*/
@JsonProperty("adult")
public void setAdult(Boolean adult) {this.adult = adult;}
/**
*
* @return
* The backdropPath
*/
@JsonProperty("backdrop_path")
public String getBackdropPath() {return backdropPath;}
/**
*
* @param backdropPath
* The backdrop_path
*/
@JsonProperty("backdrop_path")
public void setBackdropPath(String backdropPath) {this.backdropPath = backdropPath;}
/**
*
* @return
* The genreIds
*/
@JsonProperty("genre_ids")
public List<Integer> getGenreIds() {return genreIds;}
/**
*
* @param genreIds
* The genre_ids
*/
@JsonProperty("genre_ids")
public void setGenreIds(List<Integer> genreIds) {this.genreIds = genreIds;}
/**
*
* @return
* The id
*/
@JsonProperty("id")
public Integer getId() {return id;}
/**
*
* @param id
* The id
*/
@JsonProperty("id")
public void setId(Integer id) {this.id = id;}
/**
*
* @return
* The originalLanguage
*/
@JsonProperty("original_language")
public String getOriginalLanguage() {return originalLanguage;}
/**
*
* @param originalLanguage
* The original_language
*/
@JsonProperty("original_language")
public void setOriginalLanguage(String originalLanguage) {this.originalLanguage = originalLanguage;}
/**
*
* @return
* The originalTitle
*/
@JsonProperty("original_title")
public String getOriginalTitle() {return originalTitle;}
/**
*
* @param originalTitle
* The original_title
*/
@JsonProperty("original_title")
public void setOriginalTitle(String originalTitle) {this.originalTitle = originalTitle;}
/**
*
* @return
* The overview
*/
@JsonProperty("overview")
public String getOverview() {return overview;}
/**
*
* @param overview
* The overview
*/
@JsonProperty("overview")
public void setOverview(String overview) {this.overview = overview;}
/**
*
* @return
* The releaseDate
*/
@JsonProperty("release_date")
public String getReleaseDate() {return releaseDate;}
/**
*
* @param releaseDate
* The release_date
*/
@JsonProperty("release_date")
public void setReleaseDate(String releaseDate) {this.releaseDate = releaseDate;}
/**
*
* @return
* The posterPath
*/
@JsonProperty("poster_path")
public String getPosterPath() {return posterPath;}
/**
*
* @param posterPath
* The poster_path
*/
@JsonProperty("poster_path")
public void setPosterPath(String posterPath) {this.posterPath = posterPath;}
/**
*
* @return
* The popularity
*/
@JsonProperty("popularity")
public Double getPopularity() {return popularity;}
/**
*
* @param popularity
* The popularity
*/
@JsonProperty("popularity")
public void setPopularity(Double popularity) {this.popularity = popularity;}
/**
*
* @return
* The title
*/
@JsonProperty("title")
public String getTitle() {return title;}
/**
*
* @param title
* The title
*/
@JsonProperty("title")
public void setTitle(String title) {this.title = title;}
/**
*
* @return
* The video
*/
@JsonProperty("video")
public Boolean getVideo() {
return video;
}
/**
*
* @param video
* The video
*/
@JsonProperty("video")
public void setVideo(Boolean video) {this.video = video;}
/**
*
* @return
* The voteAverage
*/
@JsonProperty("vote_average")
public Double getVoteAverage() {return voteAverage;}
/**
*
* @param voteAverage
* The vote_average
*/
@JsonProperty("vote_average")
public void setVoteAverage(Double voteAverage) {this.voteAverage = voteAverage;}
/**
*
* @return
* The voteCount
*/
@JsonProperty("vote_count")
public Integer getVoteCount() {return voteCount;}
/**
*
* @param voteCount
* The vote_count
*/
@JsonProperty("vote_count")
public void setVoteCount(Integer voteCount) {this.voteCount = voteCount;}
/**
*
* @param genreIds takes list of genre Ids
* @return list of String Genres
*/
public ArrayList<String> getGenres(List<Integer> genreIds){
ArrayList<String> genres = new ArrayList<>();
for(int genreId :genreIds) {
switch (genreId) {
case 28:
genres.add("Action");
break;
case 12:
genres.add("Adventure");
break;
case 16:
genres.add("Animation");
break;
case 35:
genres.add("Comedy");
break;
case 80:
genres.add("Crime");
break;
case 99:
genres.add("Documentary");
break;
case 18:
genres.add("Drama");
break;
case 10751:
genres.add("Family");
break;
case 14:
genres.add("Fantasy");
break;
case 10796:
genres.add("Foreign");
break;
case 36:
genres.add("History");
break;
case 27:
genres.add("Horror");
break;
case 10402:
genres.add("Music");
break;
case 9648:
genres.add("Mystery");
break;
case 10747:
genres.add("Animation");
break;
case 878:
genres.add("Science Fiction");
break;
case 10770:
genres.add("TV Movie");
break;
case 53:
genres.add("Thriller");
break;
case 10752:
genres.add("War");
break;
case 37:
genres.add("Western");
break;
default:
break;
}
}
return genres;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(adult ? 1 : 0);
dest.writeString(backdropPath);
dest.writeList(genreIds);
dest.writeInt(id);
dest.writeString(originalLanguage);
dest.writeString(originalTitle);
dest.writeString(overview);
dest.writeString(releaseDate);
dest.writeString(posterPath);
dest.writeDouble(popularity);
dest.writeString(title);
dest.writeInt(video ? 1 : 0);
dest.writeDouble(voteAverage);
dest.writeInt(voteCount);
}
public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.ClassLoaderCreator<Movie>() {
@Override
public Movie createFromParcel(Parcel source) {
return new Movie(source);
}
@Override
public Movie[] newArray(int size) {
return new Movie[size];
}
@Override
public Movie createFromParcel(Parcel source, ClassLoader loader) {
return null;
}
};
}
我正在填充RecyclerView
这些对象。当我点击RecyclerView项目时,我想通过意图将该对象传递给另一个活动。但是我一直得到一个空指针。
这是我的onClick
,其中我将parcelable添加到意图
@Override
public void onClick(View v) {
Log.i("POSITION: ", String.valueOf(getAdapterPosition()));
Movie movie = mMovieList.get(getAdapterPosition());
Intent detail = new Intent(mContext, MovieDetailActivity.class);
//Bundle bundle = new Bundle();
//bundle.putParcelable("movie_object", movie);
detail.putExtra("movie_object", movie);
// detail.putExtras(bundle);
mContext.startActivity(detail);
}
评论的行是我的尝试。
在MovieDetailActivity中,我尝试访问该对象:
Intent i = getIntent();
Movie movie = i.getParcelableExtra("movie_object"); <--Null
我已经实现了serializable并且工作正常,但是,我现在想要使用需要parcelable的onSavedInstanceState方法。
我不知道出了什么问题。任何人都可以看到错误吗?
答案 0 :(得分:0)
试试这个:第一个活动直接通过意图传递mMovieList:
newlist = [(A,1),(D,4),(Y,250)]
在第二个活动中获取电影列表
Intent detail = new Intent(mContext, MovieDetailActivity.class);
intent.putParcelableArrayListExtra("movie_object", mMovieList);
mContext.startActivity(detail);
从列表中获取您的电影对象
答案 1 :(得分:0)
您可以在此处传递模型或mMovieList
Intent detail = new Intent(mContext, MovieDetailActivity.class);
intent.putParcelableArrayListExtra("movie_object", mMovieList);
startActivity(detail);
在另一项活动中你可以像这样得到它
Bundle bundle = getIntent().getExtras();
ArrayList<mMovieList> mList=bundle.getParcelableArrayList("movie_object");