Objetos personalizados en la lista AlertDialog; Cómo obtener una cadena de pantalla y luego el valor real?

He estado buscando en Android AlertDialog, y es fácil de usar el setItems (…) para agregar una lista de cadenas que se muestran.

Sin embargo, en la mayoría de los casos usted desea una lista que muestre las cadenas agradables, pero al seleccionar algo de la lista usted quiere el valor real y no la secuencia.

No he podido encontrar cómo hacer eso de una manera fácil y agradable.

¿Consejos? =)

final Button Button1 = (Button) findViewById(R.id.Button1); Button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final CharSequence[] items = { "String 1", "String 2", "String 3" }; // INstead of a string array, I want something like: // ArrayList<CustomObject> test = new ArrayList<CustomObject>(myArray); // And the CustomObject has a toString() and also a value. This array should in the best of worlds be the base for the list below =) AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle(LanguageHandler.GetString("Test")); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { // *** I want to get the value here! *** Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); } }); AlertDialog alert = builder.create(); alert.show(); } }); 

En lugar de CharSequence[] items = { "String 1", "String 2", "String 3" }; Puede utilizar un Custom Adapter en su cuadro de diálogo de alerta,

Algo como,

 AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.this); builder.setTitle("Select"); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { Toast.makeText(MyApp.this, "You selected: " + items[item],Toast.LENGTH_LONG).show(); dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); 

Su archivo list_row.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/icon" android:layout_width="48px" android:layout_height="48px" android:layout_gravity="left" /> <TextView android:id="@+id/title" android:textColor="#0000FF" android:text="" android:paddingLeft="10dip" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 

Y su ListAdapter algo así,

 String[] items = {"airplanes", "animals", "cars", "colors", "flowers", "letters", "monsters", "numbers", "shapes", "smileys", "sports", "stars" }; // Instead of String[] items, Here you can also use ArrayList for your custom object.. ListAdapter adapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.list_row, items) { ViewHolder holder; Drawable icon; class ViewHolder { ImageView icon; TextView title; } public View getView(int position, View convertView, ViewGroup parent) { final LayoutInflater inflater = (LayoutInflater) getApplicationContext() .getSystemService( Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = inflater.inflate( R.layout.list_row, null); holder = new ViewHolder(); holder.icon = (ImageView) convertView .findViewById(R.id.icon); holder.title = (TextView) convertView .findViewById(R.id.title); convertView.setTag(holder); } else { // view already defined, retrieve view holder holder = (ViewHolder) convertView.getTag(); } Drawable drawable = getResources().getDrawable(R.drawable.list_icon); //this is an image from the drawables folder holder.title.setText(items[position]); holder.icon.setImageDrawable(drawable); return convertView; } }; 
  • Cómo lograr algo como "terminar" en una clase de no actividad en android?
  • ¿Creación de AlertDialog con métodos estáticos?
  • Mostrar un temporizador de cuenta regresiva en el cuadro de diálogo de alerta
  • Botón único de AlertDialog de Android
  • Cuadro de diálogo de alerta dos botones
  • Android AlertDialog Builder
  • ¿Es posible crear una lista expandible AlertDialog?
  • El método getWindow () no está definido para el tipo AlertDialog.Builder
  • Actividad enBackPress que no muestra el diálogo de alerta
  • ¿Cómo hacer que el cuadro de diálogo personalizado parezca Diálogo de alertas?
  • El cuadro de diálogo se muestra dos veces cuando se pulsa de nuevo
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.