tobias
tobias

Reputation: 91

onBindViewHolder can´t use viewHolder

I want to fill in things like username, fullname, show visibility of the follow_btn. The big problem is that I cant use the in the onbindviewholder. It´s underlined in red. Can you help me please?

The error is: error: cannot find symbol variable follow_btn.

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

    viewHolder.follow_btn.setVisibility(View.VISIBLE);

}


@Override
public int getItemCount() {
    return User.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView username_search;
    public TextView fullname_search;
    public CircleImageView profile_pic;
    public Button follow_btn;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        username_search = itemView.findViewById(R.id.username_search);
        fullname_search = itemView.findViewById(R.id.fullname);
        profile_pic = itemView.findViewById(R.id.image_profile_pic);
        follow_btn = itemView.findViewById(R.id.follow_btn);

    }
}

Upvotes: 1

Views: 268

Answers (1)

vilpe89
vilpe89

Reputation: 4702

The problem is in the naming of class. Look closely at the method parameter, it is RecyclerView.ViewHolder. It is not your own ViewHolder class and therefore the follow_btn can't be found.

So to fix this, you need to change the type in your class declaration. You probably have something like

class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>

change it to

class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>

and fix the rest of the code now. Then you should have the onBindViewHolder-method correct:

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {...}

Upvotes: 1

Related Questions