Pregunta

Tengo una actividad TextViewer, que tiene un botón, y cuando hago clic en él Quiero un popup AlertDialog con la lista. He seguido este enlace pero no lo hace trabajo (sin ventana emergente). Creo que el contexto es incorrecto. He utilizado el siguiente código:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resources);
    ImageButton btnlist = (ImageButton)findViewById(R.id.list);
    btnlist.setOnClickListener(new View.OnClickListener() {
             public void onClick (View v){                  

              if (Vars.bookchapter>1){
               final CharSequence[] items = {"Red", "Green", "Blue"};
               Context mContext = getBaseContext();
               AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
               builder.setTitle("Pick a color");
               builder.setItems(items, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int item) {
                       Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                   }
               });
               AlertDialog alert = builder.create();
              }else{
               //Nothing
              }
             }});         
     }
        }
¿Fue útil?

Solución

No se han llamado la show() método. Haga lo siguiente:

AlertDialog alert = builder.create();
alert.show();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top