尝试使用 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;}@Overridepublic 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*/@Overridepublic int getItemViewType(int position) { if (position % CategoryActivity.ITEMS_PER_AD == 0) return BANNER_AD_VIEW_TYPE; else return MENU_ITEM_VIEW_TYPE;}
1 回答
子衿沉夜
TA贡献1828条经验 获得超3个赞
@Override public int getItemViewType(int position) { if (mCategoryList.get(position) instanceof AdView) return BANNER_AD_VIEW_TYPE; else return MENU_ITEM_VIEW_TYPE; }
添加回答
举报
0/150
提交
取消