0

我有一个代码,我在其中创建了一个方法来显示一个带有多选选项的对话框,但是当检查并单击“确定”按钮时,状态没有保存我该怎么做?我看到了一些带有共享首选项的东西,但我无法在我的代码中实现它,有人可以帮我吗?

 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();

}

4

1 回答 1

0

检查这个SO 答案。

用于loadArray在最初检索数据final boolean[] checkedgpsstoreArray在按下时保存数据public void onClick(...)

于 2020-06-05T11:53:42.630 回答