将 RecyclerView 的项目内容获取到另一个 activity
Getting contents of an item of a RecyclerView to another activity
下面的class定义了电影的内容。
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;
}
}
应用程序的第一个 activity 给出了电影专辑封面列表。渲染数据是从 API 中获取的。数据包括电影名称、上映日期、剧情简介等。但第一个 activity 仅显示海报(专辑封面)。现在,我将一个 OnclickListener 分配给 ViewHolder class,以在用户单击任何海报时启动一个新的 activity。第二个 activity 需要包含点击的电影海报的详细信息。整个数据在第一个 activity 的 AsyncTask 中获取。我可以启动新的 activity,但我无法理解如何将单击的 ViewHolder 的所需内容发送到第二个 activity。我知道数据是使用 '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);
}
}
}
提前致谢。
您可以执行 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);
}
});
}
在你的另一个 activity 中像这样获取你的 MovieCard
:
MovieCard m = (MovieCard) getIntent().getSerializableExtra("my-object");
您可以将 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);
}
在onCreate()
的MovieDetail
方法中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String artUrl = getIntent().getStringExtra("EXTRA_ART_URL");
//and so on to the rest of Strings
}
下面的class定义了电影的内容。
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;
}
}
应用程序的第一个 activity 给出了电影专辑封面列表。渲染数据是从 API 中获取的。数据包括电影名称、上映日期、剧情简介等。但第一个 activity 仅显示海报(专辑封面)。现在,我将一个 OnclickListener 分配给 ViewHolder class,以在用户单击任何海报时启动一个新的 activity。第二个 activity 需要包含点击的电影海报的详细信息。整个数据在第一个 activity 的 AsyncTask 中获取。我可以启动新的 activity,但我无法理解如何将单击的 ViewHolder 的所需内容发送到第二个 activity。我知道数据是使用 '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);
}
}
}
提前致谢。
您可以执行 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);
}
});
}
在你的另一个 activity 中像这样获取你的 MovieCard
:
MovieCard m = (MovieCard) getIntent().getSerializableExtra("my-object");
您可以将 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);
}
在onCreate()
的MovieDetail
方法中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String artUrl = getIntent().getStringExtra("EXTRA_ART_URL");
//and so on to the rest of Strings
}