如何在列表视图中添加AdMob广告?我只添加了MyAdapter类和MyViewHolder类,但出现错误。这是我的代码:public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {/// LIST ADSprivate static final int MENU_ITEM_VIEW_TYP = 0;private static final int AD_VIEW_TYPE = 1;/// LIST ADSContext c;ArrayList<Article> articles;public MyAdapter(Context c, ArrayList<Article> articles) { this.c = c; this.articles = articles;}public class NativeExpressAdsViewHoolder extends MyViewHolder { NativeExpressAdsViewHoolder(View view){ super(view); }}@Overridepublic int getItemCount() { return articles.size();}@Overridepublic long getItemId(int position) { return (position % 8 == 0) ? AD_VIEW_TYPE: position;}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { switch (viewType) { case AD_VIEW_TYPE: View nativ = LayoutInflater.from(c).inflate(R.layout.native_expres_continer, parent, false); return new MyViewHolder(nativ); case MENU_ITEM_VIEW_TYP: default: View v = LayoutInflater.from(c).inflate(R.layout.model, parent, false); return new MyViewHolder(v); }}@Overridepublic void onBindViewHolder(MyViewHolder holder, int position) { Article article=articles.get(position); NativeExpressAdsViewHoolder nativeExpressHolder = (NativeExpressAdsViewHoolder)holder; NativeExpressAdView adsView = (NativeExpressAdView)articles.get(position); Article adview=articles.get(position); String title=article.getTitle(); String desc=article.getDescription(); String date=article.getDate(); /// String imageUrl=article.getImageUrl(); holder.titleTxt.setText(title); holder.desctxt.setText(desc); holder.dateTxt.setText(date); // PicassoClient.downloadImage(c,imageUrl,holder.img);}}Myviewholder类public class MyViewHolder extends RecyclerView.ViewHolder {TextView titleTxt,desctxt,dateTxt,adview;ImageView img;public MyViewHolder(View itemView) { super(itemView); titleTxt= (TextView) itemView.findViewById(R.id.titleTxt); desctxt= (TextView) itemView.findViewById(R.id.descTxt); dateTxt= (TextView) itemView.findViewById(R.id.dateTxt); // img= (ImageView) itemView.findViewById(R.id.articleImage);}} 最佳答案 在不知道确切错误的情况下,我认为由于集成广告的方式,代码可能有问题。首先,您实际上不会真正显示列表中的所有文章,因为您只是说,每第8条文章应该显示一个广告,但是列表中原始的第8条文章不会显示。现在,我认为您的示例崩溃的位置是在您实际尝试访问onBindViewHolder,titleTxt或desctxt时在第8个元素中的dateTxt中。膨胀的ViewHolder是NativeExpressAdsViewHoolder(因为它也是RecyclerView.ViewHolder类型,所以您可以保留整个检查。holder的onBindViewHolder始终是ViewHolder),因此它不会这些字段,程序将崩溃。为了使其正常工作,您需要在提供给适配器的后备集合中具有可用广告的表示形式。因此,与其给适配器提供文章列表,不如介绍一个接口:interface ListElement { ElementType getType(); enum ElementType { ARTICLE, AD }}当然,现在,您的Article类将需要实现此接口,并始终在ElementType.ARTICLE方法中静态返回getType,并且您需要一个空类Ad,该类也将实现该接口,并始终在其方法。现在,我假设您从Web上的某个地方加载文章,然后将其传递给适配器?因此,现在将它们传递给适配器之前,请插入ElementType.AD占位符类。因此,无论您在何处安装适配器,都可以执行以下操作:private MyAdapter createAdapter(List<Article> articlesLoadedFromWeb) { final List<ListElement> articlesAndAds = new ArrayList<>(); for (int i = 0; i < articlesLoadedFromWeb.size(); i++) { if (i % 8 == 0) { articlesAndAds.add(new Ad()); } else { articlesAndAds.add(articlesLoadedFromWeb(i)); } } return new MyAdapter(getContext(), articlesAndAds);}然后您的适配器可能如下所示:public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { final Context c; final List<ListElement> articlesAndAds; public MyAdapter(Context c, List<ListElement> articlesAndAds) { this.c = c; this.articlesAndAds = articlesAndAds; } public class NativeExpressAdsViewHolder extends MyViewHolder { NativeExpressAdsViewHolder(View view){ super(view); } } @Override public int getItemCount() { return articlesAndAds.size(); } @Override public long getItemId(int position) { return articlesAndAds.get(position).getType().ordinal(); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { switch (viewType) { case ListElement.ElementType.AD.ordinal(): View nativ = LayoutInflater.from(c).inflate(R.layout.native_express_container, parent, false); return new MyViewHolder(nativ); case ListElement.ElementType.ARTICLE.ordinal(): default: View v = LayoutInflater.from(c).inflate(R.layout.model, parent, false); return new MyViewHolder(v); } } @Override public void onBindViewHolder(MyViewHolder holder, int position) { if (articlesAndAds.get(position).getType() == ListElement.ElementType.ARTICLE) { final Article article = (Article) articles.get(position); String title = article.getTitle(); String desc = article.getDescription(); String date = article.getDate(); /// String imageUrl = article.getImageUrl(); holder.titleTxt.setText(title); holder.desctxt.setText(desc); holder.dateTxt.setText(date); // PicassoClient.downloadImage(c, imageUrl, holder.img); } }}
10-05 21:52