java - Create similar Dialog when clicking different buttons -
background: have 4 buttons in activity.
what want achieve: clicking on each button displays exact same dialog list of 5 items. clicking on item in list invokes method (which defined in activity) takes 2 parameters:
- a value indicating of 4 buttons clicked
- a value indicating of 5 items in list chosen
that want achieve. think pretty common scenario can't find solution after searching awhile.
i trying follow android developer api guide: https://developer.android.com/guide/topics/ui/dialogs.html
if follow guide, have this:
public class mydialogfragment extends dialogfragment { @override public dialog oncreatedialog(bundle savedinstancestate) { alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.settitle(r.string.dialog_title) .setitems(r.array.dialog_array, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // } }); return builder.create(); } } the problem cannot find way pass value indicating button clicked onclick method. of course 1 way create 4 different dialogfragment, each each button, believe should way cleaner this. first thought create dialogfragment factory , return different anonymous dialogfragment different inputs factory, compiler complains, stating "fragments should static", apparently because of life cycle management issue. when searching around, encounter examples using oncreatedialog think can solve problem, apparently method deprecated...
i have no idea (except create 4 different dialogfragment). appreciated!
you can opposite logic
i have similar stuff, , solved using static method creates dialog , interface retrieving result:
this static method
public static void showgenericitemselectordialog(activity activity, arraylist<myparams> items, final ireturnmylistitemtoparent ireturnmylistitemtoparent) { final alertdialog.builder builder = new alertdialog.builder(activity); builder.setcancelable(true); layoutinflater inflater = activity.getlayoutinflater(); view dialogview = inflater.inflate(r.layout.dialog_searchable_list, null); //or init howerer want, have custom layout builder.setview(dialogview); final dialog dialog = builder.create(); /// omitted not useful code button mybtn= (button) dialogview.findviewbyid(r.id.mybtn); mybtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { //check item has been clicked ireturnmylistitemtoparent.onresult(myclickeditem); dialog.dismiss(); } }); dialog.setoncancellistener(new dialoginterface.oncancellistener() { @override public void oncancel(dialoginterface dialoginterface) { ireturnmylistitemtoparent.onresult(null); dialog.dismiss(); } }); dialog.show(); } this interface
public interface ireturnmylistitemtoparent { void onresult(myitem item); } this how call it:
runtimehelper.showgenericitemselectordialog(getactivity(), myparams, new ireturnmylistitemtoparent() { @override public void onresult(myitem result) { //do stuff result item clicked } }); this example, way can whatever want calling static method each button.
if want keep logic:
this way changed "pass button adapter" passing result button.
if want pass button method, add parameter in constructor points clicked button :)
hope helps
Comments
Post a Comment