I have a json-string, from it I get a defined parameter (name) and all these names are displayed via RecycleView in CardView. Everything is fine) Now I need, when I click on the card, to get additional information. And for each card has its own additional information) How to do it?

I would also like to consult with you, when you click to display additional information in a new activit or in a new fragment? How will be better and more correct?

  • In the adapter, you can hang the listener and describe it. But how to describe it? - Martinez Toni
  • setOnClickListener(new View.OnClickListener(){@Override onClick(View v){//тут клик}}); - Yuriy SPb
  • This is understandable, I am interested in how to display my info for each card. - Martinez Toni
  • additional information from the same json or from where? - Yury Pashkov p.m.
  • @ Y.Pashkov Yes, yes. Ie look. I click on the card and it opens additional information taken from Json. The bottom line is that on each card I derived the name from json, as each title has its own additional information. - Martinez Toni

2 answers 2

The adapter will have a listener as a property

 OnItemClickListener listener 

Where it can be defined as

 public interface OnItemClickListener { void onCardPressed(CardModel cardModel); } 

You can add it either through the constructor or via setListener. Next, the adapter will have something like this.

 @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CardModel cardModel = mData.get(position);// это ваша модель у которой вы имя получали для holder.name.setText if (listener != null) { listener.onPressed(cardModel); } //open new Activity/Fragment в классе, котором определен этот листенер, т.е. //активити или фрагмент, где создается этот адаптер } }); } 

For the case if there is an adapter setListener method, then in the activit / fragment where the adapter was created

 adapter.setListener(new OnItemClickListener() { @Override public void onCardClick(CardModel cardModel) { Intent intent = new Intent(this, SecondActivity.class); intent.putExtra("CardModel", cardModel); startActivity(intent); } }); 
  • What is cardModel, did not understand a bit. - Martinez Toni
  • Well, your json contains the same array of some objects, CardModel is the same object from json'a. I just called him that) - Yury Pashkov
  • So, what is this public interface OnItemClickListener {void onCardPressed (CardModel cardModel); } - Martinez Toni
  • This is just an interface that you will need to create in a project when you create a class, there you can select an interface - Yury Pashkov
  • I have too many questions, you can be found in the social. network? Would you be happy if you answered my questions? - Martinez Toni

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

  • Did, as you said, the cards do not respond to pressing - Martinez Toni
  • Noticed feature. Instantly reacts when you click on the picture, which is enough for me, since the picture takes up most of the card. Apparently in other components the event is intercepted. - BuzZer
  • Look, I got the id in the new activity, but how do I select an object from the array now? - Martinez Toni
  • I got an ID in a new activity, but I don’t know how to select an object from the array - Martinez Toni
  • From List by iterating for: protected Article getCurentArticle (List <Article> articles, String key) {int i; for (i = 0; i <articles.size (); i ++) if (articles.get (i) .getArticleId () == new Integer (key)) break; ; return articles.get (i); } where: Article is the class in which the Article object is described in me, - BuzZer