Save selected options in SharePreferences of an AlertDialog Multichoice

0

Good afternoon

I have an AlertDialog in the Multichoice actionbar, which I fill from a webservices.

I need that when the user selects one or x options and clicks on ok, those options are saved in the Sharepreferences of the system, so when the dialog is generated again, I save the selected options.

I leave you a code where I create my Alertdialog

 @Override
    protected void onPostExecute(ArrayList arrayList){
        super.onPostExecute(arrayList);

        final String[] zona = new String[ZonaArrayList.size()];
        // zona = ZonaArrayList.toArray(zona);
        for(int i=0; i<ZonaArrayList.size(); i++){
            //Obtiene el campo Descripción y lo agrega al array de strings "zona".
            zona[i] = ZonaArrayList.get(i).getDescripcion();
            // zona[i] = ZonaArrayList.get(i).getClave();
            //
        }

        AlertDialog.Builder dialog=new AlertDialog.Builder(Clientes.this);


        dialog.setTitle("Selecciona la(s) Zonas a Visitar");
        final boolean[] selZona={false,false,false};
        dialog.setMultiChoiceItems(zona,selZona,new DialogInterface.OnMultiChoiceClickListener()
        {
            @Override
            public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
                // TODO Auto-generated method stub
                if(arg2) {
                    Toast.makeText(getApplicationContext(), "Zona Seleccionada " + zona[arg1],Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialog.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                //Mandar a llamar metodo Clientes con el filtro
                SharedPreferences settings = getSharedPreferences("ONC_Settings", 0);
                AsynClien task = new AsynClien(settings.getString("ONControlWSURL", "").toString());
                //Call execute
                task.execute();
            }
        });
        dialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog alertDialog=dialog.create();
        alertDialog.show();
    }
}
    
asked by Hugo Rodriguez 08.06.2016 в 19:40
source

1 answer

1

Good afternoon we go to it, we first initialized SharedPreferences:

  SharedPreferences  sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
  SharedPreferences.Editor sharedPrefeditor= sharedPref.edit();

Save selection:

 dialog.setMultiChoiceItems(zona,selZona,new DialogInterface.OnMultiChoiceClickListener()
    {
        @Override
        public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
            // TODO Auto-generated method stub
           selZona[arg1] = !selZona[arg1]; 
        }
    });   


  dialog.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               sharedPrefeditor.putBoolean("opcion_1",  selZona[0]); 
               sharedPrefeditor.putBoolean("opcion_2",  selZona[1]);
               sharedPrefeditor.putBoolean("opcion_3",  selZona[2]);
               sharedPrefeditor.commit();
            }
        });

We start selZona with the selected data:

  final boolean[] selZona={false,false,false};
  selZona[0] = sharedPref.getBoolean("opcion_1",  false);
  selZona[1] = sharedPref.getBoolean("opcion_2",  false);
  selZona[2] = sharedPref.getBoolean("opcion_3",  false);

If you see that with this last one they are not selected you can try this:

 for (int i = 0; i < selZona.length; i++) {
     ((AlertDialog) dialog).getListView().setItemChecked(i,  selZona[i]);
 }

Greetings

    
answered by 08.06.2016 в 21:14