En el ActionBar
necesito implementar un AlertDialog
con múltiples opciones el cual se llene por medio de un ArrayList
con un Array Adapter, y los datos yo los obtengo desde un WebService implementando la clase AsyncTask
.
Necesito que me expliquen o ayude a decirme como implemento ese AlertDialog
, por el momento tengo un AlertDialog
en la pantalla que necesito con los datos de prueba que yo ingrese, pero necesito llenarlo con los datos de mi WebService.
Anexo la imagen del AlertDialog
.
Por cierto como extra necesito obtener los datos que el usuario marca y manejarlos en la Activity.
Agrego código mi menusetting
donde tengo icono que uso para abrir mi alertdialog
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="mx.com.oncontrol.oncontrolmovile.Clientes">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/action_search"
android:hint="Buscar Cliente"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
//item que uso para Alertdialog
<item
android:id="@+id/action_favorite"
android:icon="@drawable/filtro"
android:title="@string/filtro"
app:showAsAction="ifRoom"/>
</menu>
Código donde Creo el AlertDialog
con los datos ficticios
public void EligirZona(){
AlertDialog.Builder builder = new AlertDialog.Builder(Clientes.this);{
Button btn = (Button) findViewById(R.id.btn);
final TextView tv = (TextView) findViewById(R.id.tv);
// String array for alert dialog multi choice items
String[] colors = new String[]{
"Norte",
"Sur",
"Metropolitana",
};
// 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);
builder.setMultiChoiceItems(colors, 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;
// Get the current focused item
String currentItem = colorsList.get(which);
// Notify the current action
Toast.makeText(getApplicationContext(),
currentItem + " " + isChecked, Toast.LENGTH_SHORT).show();
}
});
// Specify the dialog is not cancelable
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();
}
}
Código donde mando a llamar el diálogo
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_favorite:
/*SharedPreferences settings = getSharedPreferences("ONC_Settings", 0);
AsyncZona task = new AsyncZona(settings.getString("ONControlWSURL", "").toString());
//Call execute
task.execute();*/
EligirZona();
break;
}
return super.onOptionsItemSelected(item);
}
Código donde mando a llamar el método que necesito el cual contiene la información que llenará el alertdialog
, ahora solo tengo la construcción de la clase
private class AsyncZona extends AsyncTask<String, ArrayList, ArrayList>
{
private String ONControlWSURL;
public AsyncZona(String ONControlWSURL)
{
this.ONControlWSURL = ONControlWSURL;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
AlertDialog.Builder builder = new AlertDialog.Builder(Clientes.this);
}
@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{
}
}
}
Nuevo código
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);
final ArrayList<Zona> ZonaArrayList = new ArrayList<Zona>();
String[] colors = new String[ZonaArrayList.size()];
colors = ZonaArrayList.toArray(colors);
builder.setMultiChoiceItems(colors, 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();
}
}