I am retriewing data from firebase real time database and upon clicking a new activity will be opened which will show Name, blood group and other information of the users. I want to know where should I have an OnClickListner in my recyclerview i.e. ViewHolder class or in OnBindViewHolder (Adapter). What are the pros and cons of both. Below is my code Thank You all
Adapter
public class personAdapter extends FirebaseRecyclerAdapter<
person, personAdapter.personsViewholder> {
public personAdapter(
@NonNull FirebaseRecyclerOptions<person> options)
{
super(options);
}
@Override
protected void
onBindViewHolder(@NonNull personsViewholder holder, int position, @NonNull person model)
{
holder.firstname.setText(model.getFirstname());
holder.bloodgroup.setText(model.getBloodgroup());
holder.phone.setText(model.getPhone());
}
@NonNull
@Override
public personsViewholder
onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view
= LayoutInflater.from(parent.getContext()).inflate(R.layout.person, parent, false);
return new personsViewholder(view);
}
static class personsViewholder
extends RecyclerView.ViewHolder {
TextView firstname, bloodgroup, phone;
public personsViewholder(@NonNull View itemView)
{
super(itemView);
firstname = itemView.findViewById(R.id.firstname);
bloodgroup = itemView.findViewById(R.id.bloodGroup);
phone = itemView.findViewById(R.id.phoneNumber);
}
}
}
Model
public class person
{
private String firstname;
private String phone;
private String bloodgroup;
public person(String firstname, String phone, String bloodgroup) {
this.firstname = firstname;
this.phone = phone;
this.bloodgroup = bloodgroup;
}
public person() {}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getBloodgroup() {
return bloodgroup;
}
public void setBloodgroup(String bloodgroup) {
this.bloodgroup = bloodgroup;
}