Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using different views in onCreateViewHolder

I followed this tutorial by Google Developer's YouTube channel to implement AdMob native express ads.

I got the following error:

required: packagename.adapter.viewHolder
found   : packagename.adapter.NativeExpressAdViewHolder

Here is how my onCreateViewHolder look like:

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case AD_VIEW_TYPE:
            View nativeExpressLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_express_add_container, parent, false);
            return new NativeExpressAdViewHolder(nativeExpressLayoutView); 
        case MENU_ITEM_VIEW_TYPE:
            default:
            View myLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
            return new ViewHolder(myLayoutView);
    }
}

and here is my 2 different ViewHolder classes:

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {      

    ViewHolder(View itemView) {
        super(itemView);
    }          
}

public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
    NativeExpressAdViewHolder(View view) {
        super(view);
    }
}

Here is simular questions with no answers:

  • Error "Incompatible types" while adding NativeAds in recyclerView
  • How to add NativeExpressAdView with Custom Model List Android?

EDIT:

Here is my full adapter as requested:

public class MainActivityVideoAdapter extends Adapter<MainActivityVideoAdapter.ViewHolder> {
ArrayList<Bitmap> bitmapArrayList;
Context context;
LayoutInflater layoutInflater;
View myLayoutView;
ArrayList<PathModel> ThumbPathList;
ArrayList<PathModel> VideoPathList = new ArrayList();
static DBManager manager;
long _id;

private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int AD_VIEW_TYPE = 1;

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    //Video Title
    TextView videoName;
    //Video Image
    CircularImageView videoThumb;
    //PopupMenu
    ImageButton viewholderOtions;

    ViewHolder(View itemView) {
        super(itemView);
        viewholderOtions = (ImageButton) myLayoutView.findViewById(R.id.viewholderOptions);
        videoName = (TextView) myLayoutView.findViewById(R.id.FilePath);
        videoThumb = (CircularImageView) myLayoutView.findViewById(R.id.VideoThumbnail);
        //View onClick
        itemView.setOnClickListener(this);
        //Popup onClick
        viewholderOtions.setOnClickListener(this);
    }


    //Handling click events
    @Override
    public void onClick(View v) {
        if (v == viewholderOtions) {

            int position = (int) v.getTag();

            showPopupMenu(viewholderOtions, position);
        }

    }
}

public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
    NativeExpressAdViewHolder(View view) {
        super(view);
    }
}

public MainActivityVideoAdapter(Context context, ArrayList<PathModel> ThumbPathList, ArrayList<PathModel> VideoPathList) {
    this.context = context;
    this.ThumbPathList = ThumbPathList;
    this.VideoPathList = VideoPathList;
}

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case AD_VIEW_TYPE:
            View nativeExpressLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_express_add_container, parent, false);
            return new NativeExpressAdViewHolder(nativeExpressLayoutView);
        case MENU_ITEM_VIEW_TYPE:
        default:
            View myLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
            return new ViewHolder(myLayoutView);
    }
}

public void onBindViewHolder(final ViewHolder myHolder, final int position) {
    int viewType = getItemViewType(position);
    switch (viewType) {
        case AD_VIEW_TYPE:

        case MENU_ITEM_VIEW_TYPE:
        default:

            final PathModel videoPathModel = this.VideoPathList.get(position);
            PathModel thumbathModel = this.ThumbPathList.get(position);
            File file = new File(videoPathModel.getPath());
            String filename = file.getName();
            myHolder.videoName.setText(filename);
            myHolder.videoThumb.setImageURI(Uri.parse(thumbathModel.getPath()));
            myHolder.viewholderOtions.setTag(position);

            myHolder.itemView.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {                
                Intent intent= new Intent(context, VideoPlayerActivity.class);              
                intent.putExtra("fromFA2", "fromFA2");
                context.startActivity(intent);



                }


            });

    }


}
like image 347
ClassA Avatar asked Aug 04 '26 20:08

ClassA


1 Answers

Hello @ClassA i found that you have been importing local class rather than RecyclerView.ViewHolder in onBindViewHolder()

Please go through below code, this may help you.

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

/*
------------------
your constructor goes here
-----------------
*/

 @Override
    public int getItemCount() {
        return 0;
    }

    public void onBindViewHolder(final RecyclerView.ViewHolder myHolder, final int position) {
        int viewType = getItemViewType(position);
        switch (viewType) {
            case AD_VIEW_TYPE:
            break;

            case MENU_ITEM_VIEW_TYPE:
            break;
        }
    }

   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case AD_VIEW_TYPE:
                View nativeExpressLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_express_add_container, parent, false);
                return new NativeExpressAdViewHolder(nativeExpressLayoutView);
            case MENU_ITEM_VIEW_TYPE:
                View myLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
                return new ViewHolder(myLayoutView);
        }
    }

   class ViewHolder extends RecyclerView.ViewHolder  {

        ViewHolder(View itemView) {
            super(itemView);
        }
    }

    public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
        NativeExpressAdViewHolder(View view) {
            super(view);
        }
    }
}

This code doesn't contain your variables and logic, This code is for perfect import and use of methods.

If this resolve your problem, please make this answer approved. Happy Coding.

like image 74
Amrish Kakadiya Avatar answered Aug 06 '26 09:08

Amrish Kakadiya