I have a similar problem solved as follows:
There is a static class (articles object) with List for storing objects and fields with access for the entire application, in your case, perhaps objects taken from json, then, the most subtle point is the object id binding to RecyclerView. I decided this through setting the tag to the object (as an example) TextView in this RecyclerView
@Override public void onBindViewHolder(ArticleViewHolder articleViewHolder, int position) { articleViewHolder.headerArticle.setText(articles.get(position).getHeader()); articleViewHolder.descriptionArticle.setText(articles.get(position).getDescription()); articleViewHolder.imageArticle.setImageResource(articles.get(position).getPhotoId()); //Установка идентификатора статьи articleViewHolder.headerArticle.setTag(articles.get(position).getArticleId()); }
Further, the standard Intent is called, with the transfer through the parameter of the extracted Id from the tag:
public static class ArticleViewHolder extends RecyclerView.ViewHolder { ImageView imageArticle; TextView headerArticle; TextView descriptionArticle; public ArticleViewHolder(final View itemView) { super(itemView); headerArticle = (TextView) itemView.findViewById(R.id.article_preview_header_TextView); descriptionArticle = (TextView) itemView.findViewById(R.id.article_preview_description_TextView); imageArticle = (ImageView) itemView.findViewById(R.id.article_preview_picture_ImageView); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Context context = v.getContext(); Intent intent = new Intent(context,ArticleActivity.class); intent.putExtra("idArticle",headerArticle.getTag().toString()); context.startActivity(intent); } }); } }
In the new Activity, an Id is extracted from the Intent, by which you can select an interesting object from the array / database
setOnClickListener(new View.OnClickListener(){@Override onClick(View v){//тут клик}});- Yuriy SPb ♦