javascript - how to add ads in listview in MyAdapter -
how can add admob ads in list view?
i add myadapter
class , myviewholder class error.
here code:
public class myadapter extends recyclerview.adapter<myviewholder> { /// list ads private static final int menu_item_view_typ = 0; private static final int ad_view_type = 1; /// list ads context 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); } } @override public int getitemcount() { return articles.size(); } @override public long getitemid(int position) { return (position % 8 == 0) ? ad_view_type: position; } @override public 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); } } @override public 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 class
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); }
}
without knowing exact error think code might problematic because of how integrate ads.
first never show articles in list because every 8th article should display ad instead original 8th article in list won't shown.
now think example crashes in onbindviewholder
@ 8th element when try access titletxt
, desctxt
or datetxt
on it. viewholder inflated nativeexpressadsviewhoolder
(because it's of type recyclerview.viewholder
<- btw can leave whole check. holder
of onbindviewholder
viewholder
) won't have fields , program crash.
in order work need have representation ads available in backing collection supply adapter.
so instead of giving adapter list of articles introduce interface this:
interface listelement { elementtype gettype(); enum elementtype { article, ad } }
of course article
class need implement interface , statically return elementtype.article
in gettype
method , need empty class ad
implements interface , returns elementtype.ad
in gettype
method.
now assume load articles somewhere web , pass them adapter? before passing them adapter insert ad
placeholder classes. whereever instanciate adapter this:
private myadapter createadapter(list<article> articlesloadedfromweb) { final list<listelement> articlesandads = new arraylist<>(); (int = 0; < articlesloadedfromweb.size(); i++) { if (i % 8 == 0) { articlesandads.add(new ad()); } else { articlesandads.add(articlesloadedfromweb(i)); } } return new myadapter(getcontext(), articlesandads); }
and adapter this:
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); } } }
wiki
Comments
Post a Comment