Save marked options AlertDialog Multichoice

1

Well, already fine-tuning the App that I am developing, I find myself in a small dilemma.

Good to the point, I have a screen called customer, I show a list of clients, and in the% share% I have a button which makes me ActionBar MultiChoice which I must select the areas I will visit .

Up to that point everything is fine, I get all that data from AlertDialog .

My doubt is that I need when I select an option of my MultiChoice that is saved in the WebServices and when I tap again on the filter button I show the last thing I had selected.

I leave the code where I generate my SharedPreferences MultiChoice

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();
            }
            final boolean[] selCrayons={true,false,true};
            AlertDialog.Builder dialog=new AlertDialog.Builder(Clientes.this);
            dialog.setTitle("Selecciona la(s) Zonas a Visitar");
            dialog.setMultiChoiceItems(zona,selCrayons,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

                }
            });
            dialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alertDialog=dialog.create();
            alertDialog.show();
        }
    }

I clarify the use of the class AlertDialog for the call of Asynctask .

    
asked by Hugo Rodriguez 06.06.2016 в 17:40
source

1 answer

0

The line final boolean[] selCrayons={true,false,true}; that is executed every time you create the AlertDialog is the one that always selects the same boxes. Regardless of the fact that I do not understand why you would always consult the web service, I propose this solution:

  • Make the array variable global.

    private boolean[] selCrayons = {true, false, true};
    
  • Clone the array, so as not to modify the values if you click Cancel

    protected void onPostExecute(ArrayList 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();
        }
        final boolean[] selCrayonsTemp = selCrayons.clone();
        AlertDialog.Builder dialog = new AlertDialog.Builder(Clientes.this);
        dialog.setTitle("Selecciona la(s) Zonas a Visitar");
        dialog.setMultiChoiceItems(zona, selCrayonsTemp, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
                selCrayonsTemp[arg1] = arg2;
                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) {
                selCrayons = selCrayonsTemp;
            }
        });
        dialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog alertDialog=dialog.create();
        alertDialog.show();
    }
    
  • answered by 07.06.2016 в 16:45