android - How to Populate Facebook Audience native ads within RecyclerView -
i want implement native ads facebook audience within recyclerview. ads should positioned @ particular positions in recyclerview, let start @ position 2, every 5th position recyclerviw. have have adapterclass implement recyclerview , fragment responsible showing items user.
adapterclass
public class adapterboxoffice extends recyclerview.adapter<adapterboxoffice.viewholderboxoffice> { private layoutinflater layoutinflater; private arraylist<movie> mlistmovies = new arraylist<>(); private volleysingleton volleysingleton; private imageloader imageloader; private dateformat dateformat = new simpledateformat("yyy-mm-dd"); private int previousposition=0; private linearlayout nativeadcontainer; private linearlayout adview; private adchoicesview adchoicesview; private nativead nativead; adapterview.onitemclicklistener monitemclicklistener; private arraylist<movie> filteredproductresults = new arraylist<movie>(); public adapterboxoffice(context context) { layoutinflater = layoutinflater.from(context); volleysingleton = volleysingleton.getsinstance(); imageloader = volleysingleton.getimageloader(); } public void setmovielist(arraylist<movie> listmovies) { this.mlistmovies = listmovies; notifydatasetchanged(); } public void setfilteredmovies(arraylist<movie> filteredproductresults){ this.mlistmovies=filteredproductresults; } @override public viewholderboxoffice oncreateviewholder(viewgroup parent, int viewtype) { view view=null; if(viewtype==10){ view=layoutinflater.inflate(r.layout.custom_ads_facebook, nativeadcontainer, false); nativeadcontainer.addview(adview); } else { view = layoutinflater.inflate(r.layout.custom_movie_box_office, parent, false); } viewholderboxoffice viewholder = new viewholderboxoffice(view); return viewholder; } @override public void onbindviewholder(final viewholderboxoffice holder, int position) { if (position==10){ // setting text. imageview nativeadicon = null; holder.nativeadcalltoaction.settext(nativead.getadcalltoaction()); holder.nativeadsocialcontext.settext(nativead.getadsocialcontext()); holder.nativeadtitle.settext(nativead.getadtitle()); holder.nativeadbody.settext(nativead.getadbody()); /* nativeadsocialcontext.settext(nativead.getadsocialcontext()); nativeadcalltoaction.settext(nativead.getadcalltoaction()); nativeadtitle.settext(nativead.getadtitle()) nativeadbody.settext(nativead.getadbody());*/ // downloading , setting ad icon. nativead.image adicon = nativead.getadicon(); nativead.downloadanddisplayimage(adicon, nativeadicon); // download , setting cover image. nativead.image adcoverimage = nativead.getadcoverimage(); holder.nativeadmedia.setnativead(nativead); /*nativeadmedia.setnativead(nativead);*/ // add adchoices icon if (adchoicesview == null) { adchoicesview = new adchoicesview(layoutinflater.getcontext(), nativead, true); adview.addview(adchoicesview, 0); } nativead.registerviewforinteraction(adview); }else { movie currentmovie = mlistmovies.get(position); holder.movietitle.settext(currentmovie.gettitle()); date moviereleasedate = currentmovie.getreleasedatetheater(); if (moviereleasedate != null) { string formmatteddate = dateformat.format(moviereleasedate); holder.moviereleasedate.settext(formmatteddate); } else { holder.moviereleasedate.settext(constants.na); } int audiencescore=currentmovie.getaudiencescore(); if (audiencescore==-1){ holder.movieaudiencescore.setrating(0.0f); holder.movieaudiencescore.setalpha(0.5f); } else { holder.movieaudiencescore.setrating(currentmovie.getaudiencescore() / 20.0f); holder.movieaudiencescore.setalpha(1.0f); } string urlthumbnail = currentmovie.geturlthumbnail(); loadimages(urlthumbnail, holder); } if(position>previousposition) { comeagain.materialdesign.anim.animationutils.animate(holder, true); }else { comeagain.materialdesign.anim.animationutils.animate(holder, false); } previousposition=position; } private void loadimages(string urlthumbnail, final viewholderboxoffice holder) { if (urlthumbnail != null) { imageloader.get(urlthumbnail, new imageloader.imagelistener() { @override public void onresponse(imageloader.imagecontainer response, boolean isimmediate) { holder.moviethumbnail.setimagebitmap(response.getbitmap()); } @override public void onerrorresponse(volleyerror error) { } }); } } @override public int getitemcount() { return mlistmovies.size(); } class viewholderboxoffice extends recyclerview.viewholder implements view.onclicklistener { private imageview moviethumbnail; private textview movietitle; private textview moviereleasedate; private ratingbar movieaudiencescore; private imageview nativeadicon; private textview nativeadtitle; private textview nativeadbody; private mediaview nativeadmedia; private textview nativeadsocialcontext; private button nativeadcalltoaction; public viewholderboxoffice(view itemview) { super(itemview); itemview.setonclicklistener(this); moviethumbnail = (imageview) itemview.findviewbyid(r.id.moviethumbnail); movietitle = (textview) itemview.findviewbyid(r.id.movietitle); moviereleasedate = (textview) itemview.findviewbyid(r.id.moviereleasedate); movieaudiencescore = (ratingbar) itemview.findviewbyid(r.id.movieaudiencescore); // create native ui using ad metadata. nativeadicon = (imageview)itemview.findviewbyid(r.id.native_ad_icon); nativeadtitle = (textview)itemview.findviewbyid(r.id.native_ad_title); nativeadbody = (textview)itemview.findviewbyid(r.id.native_ad_body); nativeadmedia = (mediaview)itemview.findviewbyid(r.id.native_ad_media); nativeadsocialcontext = (textview)itemview.findviewbyid(r.id.native_ad_social_context); nativeadcalltoaction = (button)itemview.findviewbyid(r.id.native_ad_call_to_action); }
fragmentclass
public class fragmentboxoffice extends fragment implements sortlistener, boxofficeloadedlistener, adlistener, nativeadsmanager.listener { // todo: rename parameter arguments, choose names match // fragment initialization parameters, e.g. arg_item_number private static final string arg_param1 = "param1"; private static final string arg_param2 = "param2"; private static final string state_movies ="state_movies" ; // todo: rename , change types of parameters private string mparam1; private string mparam2; private volleysingleton volleysingleton; private imageloader imageloader; private requestqueue requestqueue; private arraylist<movie> mlistmovies = new arraylist<>(); private dateformat dateformat = new simpledateformat("yyyy-mm-dd"); private recyclerview listmoviehits; private adapterboxoffice adapterboxoffice; private textview textvolleyerror; private moviesorter moviesorter; private nativead nativead; private linearlayout nativeadcontainer; private linearlayout adview; private adchoicesview adchoicesview; private nativeadsmanager manager; /*private swiperefreshlayout mswiperefreshlayout;*/ public fragmentboxoffice() { moviesorter=new moviesorter(); } /** * use factory method create new instance of * fragment using provided parameters. * * @param param1 parameter 1. * @param param2 parameter 2. * @return new instance of fragment fragmentboxoffice. */ // todo: rename , change types , number of parameters public static fragmentboxoffice newinstance(string param1, string param2) { fragmentboxoffice fragment = new fragmentboxoffice(); bundle args = new bundle(); args.putstring(arg_param1, param1); args.putstring(arg_param2, param2); fragment.setarguments(args); return fragment; } public void onmoviesloaded(){ } @override public view oncreateview(layoutinflater inflater, final viewgroup container, bundle savedinstancestate) { // inflate layout fragment view layout = inflater.inflate(r.layout.fragment_box_office, container, false); sethasoptionsmenu(true); textvolleyerror = (textview) layout.findviewbyid(r.id.textvolleyerror); /* mswiperefreshlayout=(swiperefreshlayout) layout.findviewbyid(r.id.swipemovieshits); mswiperefreshlayout.setonrefreshlistener(this); */ nativead=new nativead(getactivity(),"753251851443530_753848588050523"); nativead.setadlistener(this); adsettings.addtestdevice("hashed id"); nativead.loadad(); manager=new nativeadsmanager(getactivity(),"753251851443530_753848588050523", 5); manager.setlistener(this); manager.loadads(); listmoviehits = (recyclerview) layout.findviewbyid(r.id.listmoviehits); listmoviehits.setlayoutmanager(new linearlayoutmanager(getactivity())); adapterboxoffice = new adapterboxoffice(getactivity()); listmoviehits.setadapter(adapterboxoffice); if (savedinstancestate!=null) { mlistmovies=savedinstancestate.getparcelablearraylist(state_movies); }else { mlistmovies=myapplication.getwritabledatabase().readmovies(moviedatabase.box_office); if (mlistmovies.isempty()){ l.t(getactivity(),"executing task fragment"); new taskloadmoviesboxoffice(this).execute(); }else{ } } adapterboxoffice.setmovielist(mlistmovies); adapterboxoffice.setonclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { container.getcontext().startactivity(new intent(container.getcontext(), details_activity.class)); } }); return layout; } @override public void onerror(ad ad, aderror aderror) { } @override public void onadloaded(ad ad) { } @override public void onadclicked(ad ad) { } @override public void onadsloaded() { system.out.println("loaded in fragment"); nativead = manager.nextnativead(); nativead.setadlistener(this); adapterboxoffice.notifydatasetchanged(); } @override public void onaderror(aderror aderror) { }
whenever run app logcat.
01-28 17:38:27.508 14778-14778/comeagain.materialdesign e/androidruntime: fatal exception: main process: comeagain.materialdesign, pid: 14778 java.lang.nullpointerexception @ comeagain.materialdesign.adapters.adapterboxoffice.onbindviewholder(adapterboxoffice.java:99) @ comeagain.materialdesign.adapters.adapterboxoffice.onbindviewholder(adapterboxoffice.java:47)
i have struggled while now, kindly help..
Comments
Post a Comment