0

I am writing an adapter class in which the get position is showing error:

"Cannot resolve symbol 'position'" in onBindViewHolder.

public class cat_adapter extends RecyclerView.Adapter<cat_adapter.Viewholder> {

private List<cat_model> cat_modelList;

public cat_adapter(List<cat_model> cat_modelList) {
    this.cat_modelList = cat_modelList;
}


@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category,,false);
    return new Viewholder(view);
}

@Override
public void onBindViewHolder(@NonNull Viewholder holder, int i) {
    holder.setData(cat_modelList.get(position).getCategory();

}

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

class Viewholder extends RecyclerView.ViewHolder{

    private TextView title;

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

        title= itemView.findViewById(R.id.category);
    }

    private void setData(String title){
        this.title.setText(title);
    }
}


}

1 Answers1

0

Change the code like this. It will work. You have to replace 'position' with 'i'.

@Override
public void onBindViewHolder(@NonNull Viewholder holder, int i) {
    holder.setData(cat_modelList.get(i).getCategory();

}

You have to pass the variable you are getting from the second parameter in onBindViewHolder method which is a int variable.

Jarin Rocks
  • 975
  • 10
  • 17