添加 admob 横幅广告时出现 RecyclerView ClassCastException

尝试使用 RecyclerView 添加 Admob 横幅广告时出现 ClassCastException。我正在使用来自 blogger 的 JSON 数据,在添加横幅广告之前它运行良好。我正在使用此处的 GitHub 项目参考。

错误:com.example.abcd.CategoryItem 无法在 com.example.abcd.CategoryAdapter.onBindViewHolder (CategoryAdapter.java:123) 处转换为 com.google.android.ads.AdView

这是我的类别活动

类别适配器:


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

// A menu item view type.

private static final int MENU_ITEM_VIEW_TYPE = 0;

// The banner ad view type.

private static final int BANNER_AD_VIEW_TYPE = 1;


private Context mContext;


// The list of banner ads and category items.

private final List<Object> mCategoryList;



public CategoryAdapter(Context context, List<Object> CategoryList) {

    mContext = context;

    mCategoryList = CategoryList;

}


@Override

public int getItemCount() {

    return mCategoryList.size();

}




public class CategoryViewHolder extends RecyclerView.ViewHolder {

    public TextView mTextViewCategory;


    public CategoryViewHolder(View itemView) {

        super(itemView);

        mTextViewCategory = itemView.findViewById(R.id.text_view_category);

    }

}


public class AdViewHolder extends RecyclerView.ViewHolder {


    AdViewHolder(View view) {

        super(view);

    }

}


/* Determines the view type for the given position*/

@Override

public int getItemViewType(int position) {

            if (position % CategoryActivity.ITEMS_PER_AD == 0)

                return BANNER_AD_VIEW_TYPE;

            else

                return MENU_ITEM_VIEW_TYPE;

}

慕斯王
浏览 77回答 1
1回答

子衿沉夜

@Override&nbsp;public&nbsp;int&nbsp;getItemViewType(int&nbsp;position)&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(mCategoryList.get(position)&nbsp;instanceof&nbsp;AdView)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;BANNER_AD_VIEW_TYPE;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;return&nbsp;MENU_ITEM_VIEW_TYPE;&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java