java - Pass Json Object from ViewHolder to Fragment -
i have recyclerview , fragment. want pass jsonobject recyclerview fragment. so, created interface , implemented on fragment , on recyclerview. initialized variable , access method in fragment passing jsonobject it, getting npe when trying access method:
public class productresultslistfragment extends fragment implements productresultadapterinterface{ //code here @override public void showresultsinmap(jsonobject mapobject) { openmapfragment(mapobject); } in rv class have following:
public class productsearchadapter extends recyclerview.adapter<productsearchadapter.viewholder> { public productresultadapterinterface mproductresultslistener; ..... if (mapobjects.length()>0) { mproductresultslistener.showresultsinmap(mapobjects); } the if statement inside viewholder in recyclerview class instance made public.
i have tried casting mproductresultslistener dont know class cast into.
public productresultadapterinterface mproductresultslistener=((productresultadapterinterface ) ?????); just quick comment: mapobject (jsonobject) created on onclick method inside button in viewholder, cannot pass bundle on oncreate method.
this.btnmarkitemmap.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int position = getadapterposition(); switch (v.getid()) { case (r.id.markproductinmap): string mapobj=null; if (mapobjects !=null) { string positiondescription=string.valueof(position); string productlatlngvalue=txtproductprice.gettext().tostring()+";"+ txtitemlanlong.gettext().tostring(); //tring mapposition= mapobj=mapobjects.optstring(string.valueof(position)); if (mapobj!="") //button not click remove map { btnmarkitemmap.setcolorfilter(color.rgb(0, 0, 0)); btnmarkitemmap.settag(color.rgb(0, 0, 0)); mapobjects.remove(positiondescription); } else //button click add map // { btnmarkitemmap.setcolorfilter(color.rgb(255, 51, 102)); btnmarkitemmap.settag(position); try {mapobjects.put(positiondescription, productlatlngvalue);}catch (jsonexception ex){} if (mapobjects.length()>0) { mproductresultslistener.showresultsinmap(mapobjects); } } }else{ mapobjects=new jsonobject(); } break; } } });
where have assigned variable mproductresultslistener? need assign object of fragment passing adapter. otherwise value null , nullpointeexception if call function on null object.
if have, please update question code.
Comments
Post a Comment