StringListSelectDialog.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package epson.print.screen;
  2. import android.app.AlertDialog;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.os.Bundle;
  7. public class StringListSelectDialog extends DialogFragment {
  8. private static final String ARGS_KEY_STRING_LIST = "key-stringList";
  9. private OnItemSelectedListener mListener;
  10. private String[] mStringList;
  11. public interface OnItemSelectedListener {
  12. void onItemSelected(@NonNull String str);
  13. }
  14. @NonNull
  15. public static StringListSelectDialog newInstance(@NonNull String[] strArr) {
  16. StringListSelectDialog stringListSelectDialog = new StringListSelectDialog();
  17. Bundle bundle = new Bundle();
  18. bundle.putStringArray(ARGS_KEY_STRING_LIST, strArr);
  19. stringListSelectDialog.setArguments(bundle);
  20. return stringListSelectDialog;
  21. }
  22. @NonNull
  23. public Dialog onCreateDialog(@Nullable Bundle bundle) {
  24. Bundle arguments = getArguments();
  25. if (arguments != null) {
  26. mStringList = arguments.getStringArray(ARGS_KEY_STRING_LIST);
  27. if (mStringList != null) {
  28. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  29. builder.setItems(mStringList, new DialogInterface.OnClickListener() {
  30. public void onClick(DialogInterface dialogInterface, int i) {
  31. itemSelected(i);
  32. }
  33. });
  34. setCancelable(false);
  35. return builder.create();
  36. }
  37. throw new IllegalArgumentException();
  38. }
  39. throw new IllegalArgumentException();
  40. }
  41. private void itemSelected(int i) {
  42. OnItemSelectedListener onItemSelectedListener = mListener;
  43. if (onItemSelectedListener != null) {
  44. onItemSelectedListener.onItemSelected(mStringList[i]);
  45. }
  46. }
  47. public void onAttach(Context context) {
  48. super.onAttach(context);
  49. try {
  50. mListener = (OnItemSelectedListener) context;
  51. } catch (ClassCastException unused) {
  52. throw new ClassCastException(context.toString() + " must implement StringListSelectDialog.OnItemSelectedListener");
  53. }
  54. }
  55. public void onDetach() {
  56. super.onDetach();
  57. mListener = null;
  58. }
  59. }