How do I open another dialogue from a setPositiveButton?

0

I want this dialogue when you choose the items, and give you to share, send it to another confirmation dialog, so that it shows a toast with the selected elements.

This part works well:

 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

final String[] lenguajes = {"Facebook","Twitter","instagram","Google Plus","Whatsapp","Messenger","SMS"};
final boolean[] checked = {false,false,false,false,false,false,false};

builder.setTitle("Selecciona dónde quieres compartir esta aplicación");
builder.setMultiChoiceItems(lenguajes, checked, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {

    }
});
builder.setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});

builder.setPositiveButton("COMPARTIR", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        String result = "";
        int cont = 1;
        for (int i=0; i<checked.length;i++){
            if (checked[i])
                if ((cont != ((AlertDialog)dialog).getListView().getCheckedItemCount())) {
                    result += lenguajes[i]+", ";
                    cont++;
                }
                else result += lenguajes[i];
        }
        if (result == ""){
            Toast.makeText(getContext(), "Para Compartir tiene que selecionar", Toast.LENGTH_SHORT).show();
        }

Until you get to this part #

the program closes and I do not know why, or it's because maybe I have two dialogs in one.

        else {

            AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());

            builder1.setTitle("Confirmación");
            builder1.setMessage("¿Compartir esta aplicación a través de los medios seleccionados?");

            builder1.setPositiveButton("Sí", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getContext(), "Datos guardados", Toast.LENGTH_SHORT).show();
                }


            });

            builder1.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getContext(), "Datos NO guardados", Toast.LENGTH_SHORT).show();
                }

            });

        }

    }

});
return builder.create();

help I'm working on a project

    
asked by Romero Aracena 20.11.2017 в 21:56
source

2 answers

0

Change this:

 Toast.makeText(getContext(), "Datos guardados", Toast.LENGTH_SHORT).show();

Instead of getContext () use:

If you are in an Activity

 Toast.makeText(TuActividad.this, "Datos guardados", Toast.LENGTH_SHORT).show();

If you are in a Fragment

 Toast.makeText(getActivity(), "Datos guardados", Toast.LENGTH_SHORT).show();
    
answered by 20.11.2017 в 22:03
0

The most common error is the context:

If the dialog are in the activity main, the first dialog is placed as context: "this" and the second one is the one within the first, the same context of the activity is placed but as already being used (duplicating) "this" only the name of activity is placed followed by:

"this": "nombreActivity.this"
    
answered by 23.01.2019 в 16:56