Do not save checkitem selected in Sharedpreference

2

Good afternoon

I have an AlertDialog singlechoice which I fill from an array of objects which I get from a webservices.

Well my problem is that I save the id of the selected object in the shapredpreferences and then use them in a filter in a method that I send to the webservices.

everything is fine as far as I keep id of the selected object, the problem is that when I select another option it does not keep that id of that object, but it keeps saving me the same one I previously had selected.

I need to know what I'm doing wrong or what I need.

I leave my code

Alertidslog where I fill in my singlechoice and where I save the id of the object by selecting

@Override
        protected void onPostExecute(ArrayList arrayList){
            super.onPostExecute(arrayList);
            //int size = ZonaArrayList.size() + 1;
            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();
            }
                    AlertDialog.Builder alertdialog = new AlertDialog.Builder(Clientes.this);
            alertdialog.setTitle("Selecciona la Zona a Visitar");
            alertdialog.setSingleChoiceItems(zona, -1, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          // TODO Auto-generated method stub
                            SharedPreferences settings = getSharedPreferences("ONC_Settings", 0);
                            SharedPreferences.Editor editor = settings.edit();
                            editor.putInt("Clientez",ZonaArrayList.get(0).getId());
                            editor.commit();

                        }
                    })
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            //Mandar a llamar metodo Clientes con el filtro
                            SharedPreferences settings = getSharedPreferences("ONC_Settings", 0);
                            AsynClien task = new AsynClien(settings.getString("ONControlWSURL", "").toString(),settings.getInt("Clientez",1),settings.getString("ONControlToken", "").toString(),settings.getInt("ONControlEmpresa",1));
                            //Call execute
                            task.execute();
                        }
                    })
                    .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {

                        }
                    });
            AlertDialog alerdoalog = alertdialog.create();
            alerdoalog.show();
            }
    
asked by Hugo Rodriguez 11.06.2016 в 01:40
source

1 answer

1

The problem is that you are getting the value of the first element of the List (index 0):

ZonaArrayList.get(0).getId() 

use the value of wich :

ZonaArrayList.get(wich).getId() 

It would stay:

@Override
public void onClick(DialogInterface dialog, int which) {
    SharedPreferences settings = getSharedPreferences("ONC_Settings", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("Clientez",ZonaArrayList.get(wich).getId());
    editor.commit();
}  
    
answered by 11.06.2016 / 02:03
source