The class below defines contents of a movie.
public class MovieCard {
String mAlbumArtURL;
String mName;
String mSynopsis;
String mRating;
String mReleaseDate;
public MovieCard(String albumArtURL, String name, String synopsis, String rating, String releaseDate) {
this.mAlbumArtURL = albumArtURL;
this.mName = name;
this.mSynopsis = synopsis;
this.mRating = rating;
this.mReleaseDate = releaseDate;
}
public String getAlbumArtURL() {
return mAlbumArtURL;
}
public void setAlbumArtURL(String mAlbumArtURL) {
this.mAlbumArtURL = mAlbumArtURL;
}
public String getName() {
return mName;
}
public void setName(String mName) {
this.mName = mName;
}
public String getSynopsis() {
return mSynopsis;
}
public void setSynopsis(String mSynopsis) {
this.mSynopsis = mSynopsis;
}
public String getRating() {
return mRating;
}
public void setRating(String mRating) {
this.mRating = mRating;
}
public String getReleaseDate() {
return mReleaseDate;
}
public void setReleaseDate(String mReleaseDate) {
this.mReleaseDate = mReleaseDate;
}
}
The first activity of the app gives a list of movie album arts. The data for the rendering is fetched from an API. The data includes movie name, release date, plot synopsis, etc. But only the poster(album art) is shown in the first activity. Now, I assign an OnclickListener to the ViewHolder class, to start a new activity, whenever a user clicks on any of the posters. The second activity needs to contain the details of the movie poster clicked on. The entire data is fetched in the first activity's AsyncTask. I could start the new activity, but I am not able to understand how to send the required contents of the ViewHolder that is clicked on, to the second activity. I know that data is sent using 'putextra', but the issue is that i dont know what to send. I can only access the position of the ViewHolder, but not its content.
Here is the code for the adapter :
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MovieCardHolder> {
List<MovieCard> mMovieCards;
Context context;
public CustomAdapter(List<MovieCard> cards, Context c) {
this.mMovieCards = cards;
this.context = c;
}
@Override
public CustomAdapter.MovieCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_card, parent, false);
MovieCardHolder movieCardHolder = new MovieCardHolder(view, context);
return movieCardHolder;
}
@Override
public void onBindViewHolder(CustomAdapter.MovieCardHolder holder, int position) {
holder.mMovieName.setText(mMovieCards.get(position).mName);
Picasso.with(context).load(mMovieCards.get(position).mAlbumArtURL).into(holder.mMoviePoster);
}
@Override
public int getItemCount() {
return mMovieCards.size();
}
public class MovieCardHolder extends RecyclerView.ViewHolder {
ImageView mMoviePoster;
TextView mMovieName;
public MovieCardHolder(final View itemView, final Context context) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Need details of the movie to be sent to the next activity.
Intent intent = new Intent(context,MovieDetail.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
mMoviePoster = (ImageView) itemView.findViewById(R.id.movie_poster);
mMovieName = (TextView) itemView.findViewById(R.id.movie_name);
}
}
}
Thanks in advance.