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(); }