0

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;
    }
Gum Naam
  • 155
  • 13
  • Your question has nothing to do with firebase. I suggest removing that dependency so you can focus just on RecyclerView and how it owrks. – Code-Apprentice Feb 01 '22 at 17:50
  • Also, what are you adding the OnClickListener to? Is there a button in each row in the RecyclerView? Or is there a single button in the Activity which contains the RecyclerView? Or is this a listener for when you click on a row in the RecyclerView? – Code-Apprentice Feb 01 '22 at 17:56
  • It'll be a listner on each row. – Gum Naam Feb 01 '22 at 17:59
  • I want to know which is the best place to use it and why – Gum Naam Feb 01 '22 at 18:01

0 Answers0