android - Handle setOnCheckedChangeListener fired in Adapter in Fragment -


i have fragment inflates recyclerview , populates using adapter. layout has checkbox item. instead of handling checkbox item click event in adapter in onbindviewholder, want handle in fragment class implements setoncheckedchangelistener.

is possible delegate checked change event fragment?

here code -

adapter class populates cardview

public class cardtypesadapter extends recyclerview.adapter<cardtypesadapter.viewholder> {      private int[] arrcardid;     private string[] arrcardtype;     private string[] arrcardsummary;      private listener listener;      arraylist<string> selectedstrings = new arraylist<string>();      public static interface listener {         public void onclick(int position);     }      public static class viewholder extends recyclerview.viewholder {         private cardview cv;          public viewholder(cardview v) {             super(v);             cv = v;         }     }      public cardtypesadapter(int[] arrcardid, string[] arrcardtype, string[] arrcardsummary) {         this.arrcardid = arrcardid;         this.arrcardtype = arrcardtype;         this.arrcardsummary = arrcardsummary;      }       public void setlistener(listener listener) {         this.listener = listener;     }      @override     public cardtypesadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) {         cardview cv = (cardview) layoutinflater.from(parent.getcontext())                 .inflate(r.layout.card_types, parent, false);         return new viewholder(cv);     }      @override     public void onbindviewholder(viewholder holder, final int position) {          //log.d("position in bindview", ""+ position);          cardview cv = holder.cv;          final checkbox chbcardselect = (checkbox) cv.findviewbyid(r.id.chbcardselect);         chbcardselect.setid(arrcardid[position]);          chbcardselect.setoncheckedchangelistener(new oncheckedchangelistener() {             @override             public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {                   if (ischecked) {                     selectedstrings.add("" + chbcardselect.getid());                 } else {                     selectedstrings.remove("" + chbcardselect.getid());                 }                 log.d("selected id" , ""+chbcardselect.getid());              }         });           chbcardselect.setoncheckedchangelistener(new oncheckedchangelistener() {             @override             public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {                   //oncheckedchanged(buttonview, ischecked);                 if (ischecked) {                     selectedstrings.add("" + chbcardselect.getid());                 } else {                     selectedstrings.remove("" + chbcardselect.getid());                 }                 log.d("selected id" , ""+chbcardselect.getid());              }         });          textview txtcardname = (textview) cv.findviewbyid(r.id.txtcardname);         txtcardname.settext(arrcardtype[position]);          textview txtcardsummary = (textview) cv.findviewbyid(r.id.txtcardsummary);         txtcardsummary.settext(arrcardsummary[position]);          cv.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 log.d("in clicklistener", "i clicked");                 if (listener != null) {                      log.d("in clicklistener", "i clicked");                     listener.onclick(position);                  }             }         });      }      @override     public int getitemcount() {         return arrcardtype.length;     }  } 

dialogfragment class , plan receive checkbox click event

    public class cardstypefragment extends dialogfragment implements view.onclicklistener{       @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {           final view view = inflater.inflate(r.layout.fragment_card_types, container, false);          //recyclerview code         final recyclerview cardtyperecycler = (recyclerview) view.findviewbyid(r.id.cards_types_recycler);          button btnapplyfilter=(button) view.findviewbyid(r.id.btnapplyfilter);         btnapplyfilter.setonclicklistener(this);           int[] arrcardid=new int[clscardtypes.arrcardtypes.length];         string[] arrcardtype = new string[clscardtypes.arrcardtypes.length];         string[] arrcardsummary = new string[clscardtypes.arrcardtypes.length];          (int = 0; < arrcardtype.length; i++) {             arrcardid[i] = clscardtypes.arrcardtypes[i].getintcardid();             arrcardtype[i] = clscardtypes.arrcardtypes[i].getstrcardtype();             arrcardsummary[i] = clscardtypes.arrcardtypes[i].getstrcardsummary();         }          cardtypesadapter adapter = new cardtypesadapter(arrcardid, arrcardtype, arrcardsummary);         cardtyperecycler.setadapter(adapter);          linearlayoutmanager layoutmanager = new linearlayoutmanager(this.getactivity(), linearlayoutmanager.vertical,false);         cardtyperecycler.setlayoutmanager(layoutmanager);          adapter.setlistener(new cardtypesadapter.listener() {             public void onclick(int position) {                 log.d("in clicklistener", "i clicked");              }         });          return view;     }      @override     public void onclick(view view){           if(view.getid()==r.id.btnapplyfilter){              intent i=new intent()                     .putextra("name", "amit pandey")                     .putextra("age", "36");              gettargetfragment().onactivityresult(gettargetrequestcode(), activity.result_ok, i);//getactivity().getintent()             dismiss();          }     } } 

there lots of examples of communication between fragment , activity. few between adapters , fragment.

i declared interface in adapter -

public interface fragmentcommunicator{         public void passdatatofragment(string somevalue);     } 

implemented in fragment class -

public class cardstypefragment extends dialogfragment implements view.onclicklistener, cardtypesadapter.fragmentcommunicator 

passed adapter -

cardtypesadapter adapter = new cardtypesadapter(arrcardid, arrcardtype, arrcardsummary, this); 

and inside adapter -

public cardtypesadapter(int[] arrcardid, string[] arrcardtype, string[] arrcardsummary, fragmentcommunicator fragmentcommunicator) {         this.arrcardid = arrcardid;         this.arrcardtype = arrcardtype;         this.arrcardsummary = arrcardsummary;         this.fragmentcommunicator=fragmentcommunicator;      } 

and oncheckedchangelistener of checkbox

chbcardselect.setoncheckedchangelistener(new oncheckedchangelistener() {             @override             public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {                  //calling interface too-                  if(fragmentcommunicator != null)                     fragmentcommunicator.passdatatofragment("hi fragmentactivity");                 else                     log.d("msg", "not exists");              }         }); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -