亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

添加 admob 橫幅廣告時出現 RecyclerView ClassCastException

添加 admob 橫幅廣告時出現 RecyclerView ClassCastException

慕斯王 2023-02-23 16:42:47
嘗試使用 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; 
}


查看完整回答
反對 回復 2023-02-23
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號