Spinner with SharedPreferences

0

Good evening. I'm trying to save the choice the user makes in Spinner in SharedPreferences, when he clicks OK, but I'm not getting it, can anyone help me? Then I want to redeem the choice made when it opens the application again and places it in a textView.

Follow my code.

 mShowDialog = (Button) findViewById(R.id.btnEscolhaCidade);

    mShowDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
            View mView = getLayoutInflater().inflate(R.layout.dialog_spinner, null);
            mBuilder.setTitle("Em qual Cidade Você está?");

            final Spinner mSpinner = (Spinner) mView.findViewById(R.id.spinner2);
            ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_spinner_item,
                    getResources().getStringArray(R.array.cidades));
            adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mSpinner.setAdapter(adapter3);

            mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Escolha uma Cidade")) {
                        cidade_Escolhida.setText(mSpinner.getSelectedItem().toString());
                        SharedPreferences.Editor editor = preferences2.edit();
                        editor.putString(getString(R.string.cidade_escolhida_salvo), mSpinner.getSelectedItem().toString());
                        editor.apply();

                                Toast.makeText(MainActivity.this,
                                mSpinner.getSelectedItem().toString(),
                                Toast.LENGTH_LONG)
                                .show();
                        dialogInterface.dismiss();
                    }

                }
            });

            mBuilder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();

                }
            });
            mBuilder.setView(mView);
            AlertDialog dialog = mBuilder.create();
            dialog.show();
        }
    });
    
asked by anonymous 11.04.2018 / 02:40

0 answers