Pues bueno, mi problema y duda es rápida, tengo un AlertDialog
en mi ActionBar
, el cual necesito que sea multichoice.
Lo que requiero es llenar ese AlertDialog
con un arraylist
, pero mis datos yo los obtengo desde un Webservice, estoy implementando la clase Assynctask
para hacer la llamada del método y en el mismo doInbackground
lleno mi arraylist
, solo me falta saber como llenar ese arraylist que tengo a mi AlertDialog
, y que cuando el usuario seleccione una opción en mi activity
me arroje el resultado.
Dejo código de mi AlertDialog
:
public void EligirZona() {
AlertDialog.Builder builder = new AlertDialog.Builder(Clientes.this);
{
SharedPreferences settings = getSharedPreferences("ONC_Settings", 0);
AsyncZona task = new AsyncZona(settings.getString("ONControlWSURL", "").toString());
//Call execute
task.execute();
// Boolean array for initial selected items
final boolean[] checkedColors = new boolean[]{
false, // Norte
true, // Sur
false, // Metropolitana
};
// Convert the color array to list
// final List<String> colorsList = Arrays.asList(colors);
String[] zona = new String[ZonaArrayList.size()];
zona = ZonaArrayList.toArray(zona);
builder.setMultiChoiceItems(zona, checkedColors, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// Update the current focused item's checked status
checkedColors[which] = isChecked;
}
});
builder.setCancelable(false);
// Set a title for alert dialog
builder.setTitle("Selecciona la Zona(s) que Visitaras");
// Set the positive/yes button click listener
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do something when click positive button
/*tv.setText("La Zona Seleccionada es..... \n");
for (int i = 0; i<checkedColors.length; i++){
boolean checked = checkedColors[i];
if (checked) {
tv.setText(tv.getText() + colorsList.get(i) + "\n");
}
}*/
Toast.makeText(getApplicationContext(),"La Zona Seleccionada es", Toast.LENGTH_SHORT);
}
});
// Set the negative/no button click listener
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do something when click the negative button
dialog.cancel();
}
});
// Set the neutral/cancel button click listener
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do something when click the neutral button
}
});
AlertDialog dialog = builder.create();
// Display the alert dialog on interface
dialog.show();
}
}
clase AsyncZona
que uso para llamar al método:
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);
if(ZonaArrayList.size()!=0){
/*Spinner spinner = (Spinner)findViewById(R.id.menu_spinner1);
ArrayAdapter<FormaPago> adapter = new ArrayAdapter<FormaPago>(context, android.R.layout.simple_spinner_dropdown_item, PagoArrayList);
spinner.setAdapter(adapter);*/
}else{
}
}
}
Método que uso para tomar los datos:
public ArrayList<Zona> Zona()
{
String resultado = "";
SoapObject request = new SoapObject(ONControlWSNameSpace, "CatalogoGeneral");
request.addProperty("Id", 0);
request.addProperty("Clave", "");
request.addProperty("Descripcion", "");
request.addProperty("FiltroAdicional", "");
request.addProperty("Catalogo", "sp_zona");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = false;
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(ONControlWSURL, 7000);
ArrayList<Zona> ZonaArray = new ArrayList<Zona>();
try {
androidHttpTransport.debug = true;
// Invoke web service
androidHttpTransport.call(ONControlWSNameSpace+"CatalogoGeneral", envelope);
SoapObject SO_Zona = (SoapObject)((SoapObject)((SoapObject) envelope.getResponse()).getProperty(1)).getProperty(0);
for (int i = 0; i < SO_Zona.getPropertyCount(); i++) {
Zona newDetalle = new Zona(ONC_SYS.NullToZeroInteger(webService.WSGetPropertyNull("Id", (SoapObject) SO_Zona.getProperty(i))),
webService.WSGetPropertyNull("Clave", (SoapObject) SO_Zona.getProperty(i)),
webService.WSGetPropertyNull("Descripcion", (SoapObject) SO_Zona.getProperty(i)));
ZonaArray.add(newDetalle);
}
} catch (Exception e) {
//Assign Error Status true in static variable 'errored'
//menu.errored = true;
e.printStackTrace();
}
//Return booleam to calling object
//return loginStatus;
return ZonaArray;
}
agrego mi clase Zona es donde guardo mis objeto
public class Zona {
private int Id;
private String Clave;
private String Descripcion;
/*private String FiltroAdicional;
private String Catalogo;*/
public Zona(int Id, String Clave, String Descripcion)
{
super();
this.Id = Id;
this.Clave = Clave;
this.Descripcion = Descripcion;
/*this.FiltroAdicional = FiltroAdicional;
this.Catalogo = Catalogo;*/
}
public int getId() {return Id;}
public String getClave() {return Clave;}
public String getDescripcion() {return Descripcion;}
public String toString() {
return Descripcion;
}
}
Dialogo que lleno en el OnpostExecute
@Override protected void onPostExecute(ArrayList arrayList) { super.onPostExecute(arrayList);
if(ZonaArrayList.size()!=0) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Selecciona Zona que Visitaras");
//builder.setMessage("Selecciona la Zona(s) que Visitaras");
adapter = new ArrayAdapter<Zona>(context, android.R.layout.simple_list_item_multiple_choice, ZonaArrayList);
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();
// adapter = new ArrayAdapter<Zona>(context, android.R.layout.select_dialog_multichoice, ZonaArrayList);
/*Spinner spinner = (Spinner)findViewById(R.id.menu_spinner1);
ArrayAdapter<FormaPago> adapter = new ArrayAdapter<FormaPago>(context, android.R.layout.simple_spinner_dropdown_item, PagoArrayList);
spinner.setAdapter(adapter);*/
}else{
Logcat
06-03 10:43:15.837 1536-1536/mx.com.oncontrol.oncontrolmovile D/dalvikvm: GC_FOR_ALLOC freed 729K, 32% free 5102K/7428K, paused 7ms, total 7ms
06-03 10:43:15.889 1536-1536/mx.com.oncontrol.oncontrolmovile W/EGL_genymotion: eglSurfaceAttrib not implemented
06-03 10:43:15.897 1536-1536/mx.com.oncontrol.oncontrolmovile W/ViewRootImpl: Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=262, eventTime=1057598, downTime=1039632, deviceId=1, source=0x301 }
06-03 10:43:16.025 1536-1536/mx.com.oncontrol.oncontrolmovile W/EGL_genymotion: eglSurfaceAttrib not implemented
06-03 10:43:16.029 1536-1536/mx.com.oncontrol.oncontrolmovile W/ViewRootImpl: Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=264, eventTime=1057700, downTime=1039632, deviceId=1, source=0x301 }
06-03 10:43:16.029 1536-1536/mx.com.oncontrol.oncontrolmovile W/ViewRootImpl: Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=265, eventTime=1057752, downTime=1039632, deviceId=1, source=0x301 }
06-03 10:43:16.205 1536-1536/mx.com.oncontrol.oncontrolmovile W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
06-03 10:43:16.209 1536-1536/mx.com.oncontrol.oncontrolmovile W/ViewRootImpl: Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=269, eventTime=1057958, downTime=1039632, deviceId=1, source=0x301 }
[ 06-03 10:43:16.405 64: 64 D/ ]
Socket deconnection
[ 06-03 10:43:18.421 64: 64 D/ ]
Socket deconnection
[ 06-03 10:43:20.429 64: 64 D/ ]
Socket deconnection
[ 06-03 10:43:22.441 64: 64 D/ ]
Socket deconnection
}