I have an AlertDialog that has a multichoice and that it is filled by means of an arraylist of objects, that data I get from a webservices.
The problem I have with this AlertDialog is that when I click on my actionbar to see if it loads the correct data, but when I try to tap the check I need, I do not select anything and I close the alertDialog and he does not send me an error in the logcat.
I clarify the AlertDialog I believe in the OnpostExecute since I am using Asyntask
I leave code
Class Asyntask, where I generate my alertDialog
private class AsyncZona extends AsyncTask<String, ArrayList, ArrayList>
{
private String ONControlWSURL;
public AsyncZona(String ONControlWSURL)
{
this.ONControlWSURL = ONControlWSURL;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected ArrayList doInBackground(String... params) {
ZonaArrayList = new ArrayList<Zona>();
webService O_WS = new webService(ONControlWSURL);
ZonaArrayList=O_WS.Zona();
return null;
}
@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();
}
final boolean[] isSelectedArray ={false,false,false,false,false,false};
AlertDialog.Builder builder = new AlertDialog.Builder(Clientes.this);
builder.setTitle("Selecciona la Zona(s) que Visitaras");
builder.setMultiChoiceItems(zona, isSelectedArray, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked)
{
Toast.makeText(getApplicationContext(),"Seleccionado" + zona[which],Toast.LENGTH_SHORT).show();
}
}
});
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}
});
builder.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do something
}
});;
builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Solution I leave the code
@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();
}
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
}
});
dialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog=dialog.create();
alertDialog.show();
}
}