我如何将多选状态保存在警报对话框中?

问题描述 投票:0回答:1

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

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

}

java android android-studio android-alertdialog multichoiceitems
1个回答
0
投票

检查this这样的答案。

使用loadArray最初在final boolean[] checkedgps中检索数据然后按storeArray

中的public void onClick(...)保存数据
© www.soinside.com 2019 - 2024. All rights reserved.