0

I have a code where I created a method to display a dialog box with the option of multi-choice but when checking and clicking the OK button the status is not saved how can I do this? I saw something with sharedpreferences but I'm not able to implement it in my code could someone help me?

 public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    final View view =   inflater.inflate(R.layout.fragment_inseticida, container, false);


    gps = view.findViewById(R.id.imageButton);
        gps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAlertDialog();
            }
        });



private void showAlertDialog (){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Habilitar GPS");

        String[] Gps = new String[]{
            "Habilitar GPS",

        };

        // Boolean array for initial selected items
        final boolean[] checkedgps = new boolean[]{
            true, // GPS

        };

        // Convert the color array to list
        final List<String> gpsList = Arrays.asList(Gps);

        builder.setMultiChoiceItems(Gps, checkedgps, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {


                checkedgps[i] = b;

                String CurrentItem  = gpsList.get(i);

                Toast.makeText(getActivity(),CurrentItem +  " " + b,Toast.LENGTH_SHORT).show();



            }
        });
        builder.setCancelable(false);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        AlertDialog dialog = builder.create();
        // Display the alert dialog on interface
        dialog.show();

}

2 Answers2

0

Check this SO answer.

Use loadArray to retrieve the data initially in final boolean[] checkedgps And storeArray to save the data when pressed in public void onClick(...)

Simon
  • 1,657
  • 11
  • 16
0

save the multichoice state in an alertdialog using sharedpreferences check SharedPreferences on Android docs. and it's better to use MaterialAlertDialogBuilder

devio
  • 607
  • 9
  • 29
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32314147) – kamentk Jul 26 '22 at 07:42