androidandroid-recyclerviewincompatibletypeerror

incompatible types:NewFeedAdapter.MyAdViewHolder cannot be converted to NewFeedAdapter.ViewHolder


I try to use admob into the recyclerView, but I got the error.

error: incompatible types: NewFeedAdapter.MyAdViewHolder cannot be converted to NewFeedAdapter.ViewHolder return myAdViewHolder;

this is my adapter code

public class NewFeedAdapter extends RecyclerView.Adapter<NewFeedAdapter.ViewHolder> {

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType)
        {
            case ITEM_TYPE_BANNER:
                View bannerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.google_ads_container, parent, false);
                MyAdViewHolder myAdViewHolder = new MyAdViewHolder(bannerView);
                return myAdViewHolder;
            default:
            case ITEM_TYPE_FEED:
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ifunpot_feed, parent, false);
                ViewHolder viewHolder = new ViewHolder(v);
                return viewHolder;
        }
}

class MyAdViewHolder extends RecyclerView.ViewHolder{
        public MyAdViewHolder(@NonNull View itemView)
        {
            super(itemView);
        }
    }
}

why cannot return myAdViewHolder?

incompatible types: Required: com.example.example.adapter.NewFeedAdapter.ViewHolder Found: com.example.example.adapter.NewFeedAdapter.MyAdViewHolder


Solution

  • You should change :

    public class NewFeedAdapter extends RecyclerView.Adapter<NewFeedAdapter.ViewHolder>
    

    to :

    public class NewFeedAdapter extends RecyclerView.Adapter<NewFeedAdapter.MyAdViewHolder>