下面的课程定义了电影的内容。
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;
}
}
该应用程序的第一个活动提供了电影专辑艺术列表。渲染的数据是从API获取的。数据包括电影名称,发布日期,情节概要等。但是只有海报(专辑封面)显示在第一个活动中。现在,我将一个OnclickListener分配给ViewHolder类,以便在用户点击任何海报时开始新的活动。第二个活动需要包含点击的电影海报的详细信息。整个数据在第一个活动的AsyncTask中获取。我可以开始新的活动,但是我无法理解如何将所点击的ViewHolder的所需内容发送到第二个活动。我知道数据是使用'putextra'发送的,但问题是我不知道要发送什么。我只能访问ViewHolder的位置,但不能访问其内容。
以下是适配器的代码:
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);
}
}
}
提前致谢。
答案 0 :(得分:1)
您可以将MovieCard
附加到根View
;
public class MovieCardHolder extends RecyclerView.ViewHolder {
View root;
ImageView mMoviePoster;
TextView mMovieName;
public MovieCardHolder(final ViewGroup itemView, final Context context) {
super(itemView);
root = itemView;
root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Need details of the movie to be sent to the next activity.
MovieCard card = (MovieCard)v.getTag();
Intent intent = new Intent(context, MovieDetail.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("EXTRA_ART_URL", card.getAlbumArtURL());
intent.putExtra("EXTRA_NAME", card.getName());
intent.putExtra("EXTRA_Synopsis", card.getSynopsis());
intent.putExtra("EXTRA_Rating", card.getRating());
intent.putExtra("EXTRA_ReleaseDate", card.getReleaseDate());
context.startActivity(intent);
}
});
mMoviePoster = (ImageView) root.findViewById(R.id.movie_poster);
mMovieName = (TextView) root.findViewById(R.id.movie_name);
}
}
和
@Override
public void onBindViewHolder(CustomAdapter.MovieCardHolder holder, int position) {
holder.root.setTag(mMovieCards.get(position));
holder.mMovieName.setText(mMovieCards.get(position).mName);
Picasso.with(context).load(mMovieCards.get(position).mAlbumArtURL).into(holder.mMoviePoster);
}
使用MovieDetail
onCreate()
方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String artUrl = getIntent().getStringExtra("EXTRA_ART_URL");
//and so on to the rest of Strings
}
答案 1 :(得分:0)
您可以mMovieCards.get(position)
来获取所需的MovieCard
。
您需要通过实施将MovieCard
声明为Serializable
,如下所示:
class MovieCard implements Serializable
并通过putExtras(String key, Serializable value)
下的Intent
发送,最后使用onBindViewHolder()
回调。
@Override
public void onBindViewHolder(CustomAdapter.MovieCardHolder holder, int position) {
// maybe declare a ViewGroup class member in your holder
holder.parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, AnotherActivity.class);
intent.putExtra("my-object", mMovieCards.get(position));
context.startActivity(intent);
}
});
}
在您的其他活动中,您可以像这样获取MovieCard
:
MovieCard m = (MovieCard) getIntent().getSerializableExtra("my-object");