I use the RecyclerView list, the items consist of a cardview, each of which has text and a button. With the click handler on the item itself everything seems to be clear

public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener { public Holder(View itemView) { super(itemView); itemView.setOnClickListener(this); } @Override public void onClick(View v) { Context context = v.getContext(); Intent ddddd = new Intent(context, LastActivity.class); long yy = personsFitr.get(getAdapterPosition()).id; String str = Long.toString(yy); ddddd.putExtra("id", str); context.startActivity(ddddd); } } 

And how to handle exactly the button click on the item?


More detailed

Created a data model

Person

 public class Person { long id; String title; String prich; String affirm; String favorite; int photoId; 

Fill out the data model from the database (using Sugar ORM)

 // Π·Π°ΠΏΠΎΠ»Π½ΠΈΠ» модСль Π΄Π°Π½Π½Ρ‹Ρ… persons = new ArrayList<>(); for(Contact contact:allContacts){ persons.add(new Person(contact.getId(), contact.title, contact.prich, contact.affirm, contact.favorite, contact.photoId)); } 

In the adapter I get it all. Before this, when I clicked on an item in the application, I switched to the second Activiti where I passed the id, in the second Activiti I received an id, and by id I’d already output data from the database.

Also in the second Activiti there was a button, when clicked, the item was added to the Favorites.

But it would not be very beautiful to add to your favorites, right from the item in the list, for example, if you press a button.

Adapter code

 public class CheeseAdapter extends RecyclerView.Adapter<CheeseAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener { CardView cv; TextView title; TextView title2; ImageView personPhoto; Button button; public Holder(View itemView) { super(itemView); cv = (CardView)itemView.findViewById(R.id.cv); title = (TextView) itemView.findViewById(R.id.cheeseName); title2 = (TextView) itemView.findViewById(R.id.person_age); personPhoto = (ImageView) itemView.findViewById(R.id.person_photo22); button = (Button) itemView.findViewById(R.id.button); itemView.setOnClickListener(this); } @Override public void onClick(View v) { Context context = v.getContext(); Intent ddddd = new Intent(context, LastActivity.class); long yy = personsFitr.get(getAdapterPosition()).id; String str2 = Long.toString(yy); ddddd.putExtra("id", str2); context.startActivity(ddddd); Toast.makeText(v.getContext(), personsFitr.get(getAdapterPosition()).title, Toast.LENGTH_SHORT).show(); } } private final LayoutInflater mInflater; List<Person> persons; List<Person> personsFitr; // public CheeseAdapter(LayoutInflater inflater, List<Person> persons11) { mInflater = inflater; persons = persons11; personsFitr=persons; } @Override public Holder onCreateViewHolder(ViewGroup parent, int viewType) { return new Holder(mInflater.inflate(R.layout.item_layout, parent, false)); } @Override public void onBindViewHolder(Holder holder, int position) { View itemView = holder.itemView; holder.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Π½ΡƒΠΆΠ½Ρ‹ΠΉ ΠΊΠΎΠ΄ Context context = v.getContext(); Toast.makeText(v.getContext(), "тСкст", Toast.LENGTH_SHORT).show(); } }); holder.title.setText(personsFitr.get(position).title); holder.title2.setText(personsFitr.get(position).prich); holder.personPhoto.setImageResource(personsFitr.get(position).photoId); } @Override public int getItemCount() { return personsFitr.size(); } 
  • You need to bring the receipt of references to the elements of the item from the adapter to the holder (otherwise the meaning of the holder is completely lost) and in the holder hang handlers on them. See the holder in this question - here click on the whole item and separately on the button that displays the popup. - pavlofff
  • Thank. I 'll try to figure it out - Artsait

2 answers 2

Like that:

Favorite field must be of type boolean , not String

 public class CheeseAdapter extends RecyclerView.Adapter<CheeseAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { CardView cv; TextView title; TextView title2; ImageView personPhoto; Button button; public Holder(View itemView) { super(itemView); cv = (CardView)itemView.findViewById(R.id.cv); title = (TextView) itemView.findViewById(R.id.cheeseName); title2 = (TextView) itemView.findViewById(R.id.person_age); personPhoto = (ImageView) itemView.findViewById(R.id.person_photo22); button = (Button) itemView.findViewById(R.id.button); } } private final LayoutInflater mInflater; List<Person> persons; List<Person> personsFitr; public CheeseAdapter(LayoutInflater inflater, List<Person> persons11) { mInflater = inflater; persons = persons11; personsFitr=persons; } @Override public Holder onCreateViewHolder(ViewGroup parent, int viewType) { return new Holder(mInflater.inflate(R.layout.item_layout, parent, false)); } @Override public void onBindViewHolder(Holder holder, int position) { holder.title.setText(personsFitr.get(position).title); holder.title2.setText(personsFitr.get(position).prich); holder.personPhoto.setImageResource(personsFitr.get(position).photoId); // ΠΎΠΊΡ€Π°ΡˆΠΈΠ²Π°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΠΊΡƒ для выдСлСния, Ρ‡Ρ‚ΠΎ элСмСнт Π² ΠΈΠ·Π±Ρ€Π°Π½Π½ΠΎΠΌ if (personsFitr.get(position).favorite == false) { holder.button.setBackgroundColor(#FFAAAA); //Ρ†Π²Π΅Ρ‚ Π½Π΅ Π² ΠΈΠ·Π±Ρ€Π°Π½ΠΎΠΌ } else { holder.button.setBackgroundColor(#AAAAAA); // Ρ†Π²Π΅Ρ‚ Π² ΠΈΠ·Π±Ρ€Π°Π½Π½ΠΎΠΌ } holder.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Context context = v.getContext(); long id = personsFitr.get(position).id; // ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ ΠΈΠ½Π²Π΅Ρ€Ρ‚ΠΈΡ€ΡƒΠ΅ΠΌ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Π² ΠΈΠ·Π±Ρ€Π°Π½Π½ΠΎΠ΅, // ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ Π½Π°ΠΆΠ°Ρ‚ΠΈΠ΅ добавляСт\ΡƒΠ±ΠΈΡ€Π°Π΅Ρ‚ ΠΈΠ· ΠΈΠ·Π±Ρ€Π°Π½Π½ΠΎΠ³ΠΎ personsFitr.get(position).favorite = !personsFitr.get(position).favorite; // Π—Π΄Π΅ΡΡŒ Π΄Π΅Π»Π°Π΅ΠΌ запись Π² Π‘Π” ΠΎ ΠΈΠ·Π±Ρ€Π°Π½Π½ΠΎΠΌ, значСния элСмСнта с ID = id // ЗаписываСм Π² Π‘Π” Π² ΠΏΠΎΠ·ΠΈΡ†ΠΈΡŽ id Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ personsFitr.get(position).favorite // обновляСм элСмСнт Π² спискС, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΠΎΠ±Ρ€Π°Π·ΠΈΡ‚ΡŒ Π½ΠΎΠ²ΠΎΠ΅ состояниС notifyItemChanged(position); } }); } @Override public int getItemCount() { return personsFitr.size(); } } 

    For example for title :

    In onBindViewHolder()

      holder.title.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Π½ΡƒΠΆΠ½Ρ‹ΠΉ ΠΊΠΎΠ΄ } }); 

    And so for each field. The point is that the holder holds links to each item, and accessing them is no different, as if processed in a regular class.

    • The question in the holder does not store references to the elements of the item, they are obtained in the adapter itself, which is not correct and because of this it is impossible to hang handlers in them in the holder. - pavlofff
    • Did not quite understand how to keep references to the elements of the item in the holder ?? - Artsait