I decided to dig into RecyclerView, everything seems to be fine, but for some reason, when the onBindViewHolder () method is overloaded, ide swears on the myViewHolder holder parameter, it wants me to use RecyclerView.ViewHolder. At the same time, when the onCreateViewHolder () method is overloaded, there are no problems.

Here is the code itself:

public class RegionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ public List<Region> regions; public RegionAdapter(List<Region> regions){ this.regions = regions; } class MyViewHolder extends RecyclerView.ViewHolder{ private TextView name; public MyViewHolder(View itemView) { super(itemView); name = (TextView) itemView.findViewById(R.id.cityRecyclerViewItemName); } } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new MyViewHolder(LayoutInflater.from(parent.getContext()). inflate(R.layout.region_recyclerview_item, parent, false)); } @Override public void onBindViewHolder(MyViewHolder holder, int position){ holder.name.setText(regions.get(position).getName()); } @Override public int getItemCount() { return regions.size(); } } 
  • You declare the class class MyViewHolder extends RecyclerView.ViewHolder, but the public method MyViewHolder onCreateViewHolder is removed from it, which is probably why the studio is trying to apply RecyclerView.ViewHolder to it. try to add a method to the class body. - ZigZag
  • the onСreateViewHolder method belongs to the RecyclerView.Adapter so I overload it in the RegionAdapter, and there are no problems with it :) An error in onBindViewHolder, for some reason, I want to give it RecyclerView.ViewHolder, not my ViewHolder version. I did this on habrahabr.ru/post/237101 - JJoe

1 answer 1

You incorrectly reproduced an example from an article from a habr . When describing an adapter class, you must specify the type of adapter holder. The default (and you) is RecyclerView.ViewHolder . And it is necessary (and in an article like this): YoursAdapterClassName<YoursAdapterClassName.YoursHolderClassName> - i.e. Specify not the default, but your custom type of holder. Those. you should be able to

 public class RegionAdapter extends RecyclerView.Adapter<RegionAdapter.MyViewHolder> 

Your current error itself is caused by the fact that when overriding superclass methods, you can replace the return type with a subclass, but you cannot do this with method parameters - this is one of the language / OOP restrictions.