riyanwenas

FormatSelectorDialogFragment

Feb 15th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. public class FormatSelectorDialogFragment extends DialogFragment {
  2. public interface FormatSelectorDialogListener {
  3. public void onFormatsSaved(ArrayList<Integer> selectedIndices);
  4. }
  5.  
  6. private ArrayList<Integer> mSelectedIndices;
  7. private FormatSelectorDialogListener mListener;
  8.  
  9. public void onCreate(Bundle state) {
  10. super.onCreate(state);
  11. setRetainInstance(true);
  12. }
  13.  
  14. public static FormatSelectorDialogFragment newInstance(FormatSelectorDialogListener listener, ArrayList<Integer> selectedIndices) {
  15. FormatSelectorDialogFragment fragment = new FormatSelectorDialogFragment();
  16. if (selectedIndices == null) {
  17. selectedIndices = new ArrayList<Integer>();
  18. }
  19. fragment.mSelectedIndices = new ArrayList<Integer>(selectedIndices);
  20. fragment.mListener = listener;
  21. return fragment;
  22. }
  23.  
  24. @Override
  25. public Dialog onCreateDialog(Bundle savedInstanceState) {
  26. if (mSelectedIndices == null || mListener == null) {
  27. dismiss();
  28. return null;
  29. }
  30.  
  31. String[] formats = new String[ZXingScannerView.ALL_FORMATS.size()];
  32. boolean[] checkedIndices = new boolean[ZXingScannerView.ALL_FORMATS.size()];
  33. int i = 0;
  34. for (BarcodeFormat format : ZXingScannerView.ALL_FORMATS) {
  35. formats[i] = format.toString();
  36. if (mSelectedIndices.contains(i)) {
  37. checkedIndices[i] = true;
  38. } else {
  39. checkedIndices[i] = false;
  40. }
  41. i++;
  42. }
  43.  
  44. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  45. // Set the dialog title
  46. builder.setTitle(R.string.choose_formats)
  47. // Specify the list array, the items to be selected by default (null for none),
  48. // and the listener through which to receive callbacks when items are selected
  49. .setMultiChoiceItems(formats, checkedIndices,
  50. new DialogInterface.OnMultiChoiceClickListener() {
  51. @Override
  52. public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  53. if (isChecked) {
  54. // If the user checked the item, add it to the selected items
  55. mSelectedIndices.add(which);
  56. } else if (mSelectedIndices.contains(which)) {
  57. // Else, if the item is already in the array, remove it
  58. mSelectedIndices.remove(mSelectedIndices.indexOf(which));
  59. }
  60. }
  61. })
  62. // Set the action buttons
  63. .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
  64. @Override
  65. public void onClick(DialogInterface dialog, int id) {
  66. // User clicked OK, so save the mSelectedIndices results somewhere
  67. // or return them to the component that opened the dialog
  68. if (mListener != null) {
  69. mListener.onFormatsSaved(mSelectedIndices);
  70. }
  71. }
  72. })
  73. .setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
  74. @Override
  75. public void onClick(DialogInterface dialog, int id) {
  76. }
  77. });
  78.  
  79. return builder.create();
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment