java - Implementing a Callback for DialogFragment -


from recylerview adapter, there multiple buttons in every items, want show dialogfragment confirmation when clicked , execute different methods/actions when positivebutton clicked.

the problem don't know how can have or how can implement callback when positivebutton clicked , can execute different methods or actions according button clicked on every items.

e.g. on adapter class

@override public void onbindviewholder(final pageonlineadapter.theviewholder holder, final int position) {      holder.btn_start.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             dialogfragment newfragment = confirmationdialog.newinstance(title,content); //initialize dialogfragment specific title , content             newfragment.show(((appcompatactivity)contextview).getsupportfragmentmanager(), tag);              /*                 if positivebutton clicked                     =>execute method/action here             */         }     );      holder.btn_stop.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             dialogfragment newfragment = confirmationdialog.newinstance(title,content); //initialize dialogfragment specific title , content             newfragment.show(((appcompatactivity)contextview).getsupportfragmentmanager(), tag);              /*                 if positivebutton clicked                     =>execute method/action here             */           }     );      holder.btn_close.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             dialogfragment newfragment = confirmationdialog.newinstance(title,content); //initialize dialogfragment specific title , content             newfragment.show(((appcompatactivity)contextview).getsupportfragmentmanager(), tag);              /*                 if positivebutton clicked                     =>execute method/action here             */           }     );      holder.btn_remove.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             dialogfragment newfragment = confirmationdialog.newinstance(title,content); //initialize dialogfragment specific title , content             newfragment.show(((appcompatactivity)contextview).getsupportfragmentmanager(), tag);              /*                 if positivebutton clicked                     =>execute method/action here             */          }     ); } 

here's dialogfragment class

public class confirmationdialog extends dialogfragment {      public static confirmationdialog newinstance(string title, string description) {         confirmationdialog frag = new confirmationdialog();         bundle args = new bundle();         args.putstring("title", title);         args.putstring("description", description);         frag.setarguments(args);         return frag;     }      @override     public dialog oncreatedialog(bundle savedinstancestate) {         string title = getarguments().getstring("title");         string description = getarguments().getstring("description");         return new alertdialog.builder(getactivity())                 //.seticon(r.drawable.alert_dialog_icon)                 .settitle(title)                 .setmessage(description)                 .setpositivebutton("continue",                         new dialoginterface.onclicklistener() {                             public void onclick(dialoginterface dialog, int whichbutton) {                                //how tell when positivebutton clicked                             }                         }                 )                 .setnegativebutton("cancel",                         new dialoginterface.onclicklistener() {                             public void onclick(dialoginterface dialog, int whichbutton) {                                 //how tell when negativebutton clicked                             }                         }                 )                 .create();     } } 

an option create interface going implemented adapter , passed dialogframe when creating it. dialogframe invoke method when positive clicked , 1 when negative clicked :

public interface dialogcallback{      void clickedpositivestart();     void clickedpositivestop();     void clickednegativestart();     void clickednegativestop(); }     public class myadapter extends adapter implements dialogcallback, view.onclicklistener{     @override    protected void onclick(view view){        dialogfragment newfragment =  null;        string action;         if(view.getid() == r.id.your_start_button){             action = "start";        }else if(view.getid() == r.id.your_stop_button){            action = "stop";            }else if(..){            ....        }else{            return;        }         newfragment = confirmationdialog.newinstance(title,content, this, action);        newfragment.show(((appcompatactivity)contextview).getsupportfragmentmanager(), tag);     }     @override    public void clickedpositivestart(){         // when positive clicked    }     @override    public void clickedpositivestop(){         // when positive clicked    }     @override    public void clickednegativestart(){         // when negative clicked    }     @override    public void clickednegativestop(){         // when negative clicked    }  }   public confirmationdialog extends dialogfragment{      private dialogcallback callback;     private string action;      public static confirmationdialog newinstance(string title, string description, dialogcallback callback, string action) {         this.callback = callback;         this.action = action;         ...     }   @override public dialog oncreatedialog(bundle savedinstancestate) {     string title = getarguments().getstring("title");     string description = getarguments().getstring("description");     return new alertdialog.builder(getactivity())             //.seticon(r.drawable.alert_dialog_icon)             .settitle(title)             .setmessage(description)             .setpositivebutton("continue",                     new dialoginterface.onclicklistener() {                         public void onclick(dialoginterface dialog, int whichbutton) {                            if(action.equals("start"){                               callback.clickedpositivestart();                            } else if(action.equals("stop"){                               callback.clickedpositivestop();                            }                         }                     }             )             .setnegativebutton("cancel",                     new dialoginterface.onclicklistener() {                         public void onclick(dialoginterface dialog, int whichbutton) {                             if(action.equals("start"){                               callback.clickednegativestart();                            } else if(action.equals("stop"){                               callback.clickednegativestop();                            }                         }                     }             )             .create();      }  }  } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -