Retrieving EditText text in AlertDialog

0

Good afternoon, I have a problem ... Clicking a button brings up the custom alertDialog, that is, it contains an EditText in it and a positive button for completion. Only I need to retrieve the text of the editText so I can insert it into my database and I'm not getting it. I put in Log nothing appears, so I debugged, I put a breakpoint to see if it was at least saving a getText() and when I go to see it it inserts the value as empty "

I do not know what to do, could someone help me? I did the getText (). ToString and nothing of value ....

code:

private AlertDialog.Builder dialogs;
private String tsl;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        golFora = (ImageButton) findViewById(R.id.golForaId);

        golFora.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador1++;
                valorDouble.setText("" + contador1);

                Context gol = getApplication();
                CharSequence texto = "Goooll!";
                int tempo = Toast.LENGTH_SHORT;
                Toast apresentar = Toast.makeText(gol, texto, tempo);
                apresentar.show();

View vi = LayoutInflater.from(getApplicationContext()).inflate(R.layout.caixa_jogador_dialog, null);

                teset = (EditText) vi.findViewById(R.id.caixa_dialog_1);

                //criado só para o Log
                tsl = teset.getText().toString();

                dialogs = new AlertDialog.Builder(SimplesHome.this);
                dialogs.setPositiveButton("Sim", new 
                DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                     Log.i("CAMPOS", tsl);

                        Esporte s = new Esporte();
                        s.setJogadoresFora(teset.getText().toString());
                    }
                });

                dialogs.setView(vi);

                dialogs.create();
                dialogs.show();

            }
        });

Thank you ....

    
asked by anonymous 13.07.2017 / 21:36

1 answer

2

Use the Dialog object passed to the onClick() method of the DialogInterface.OnClickListener to get the reference to EditText:

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

     Log.i("CAMPOS", tsl);

        EditText caixa_dialog_1 = (EditText) ((Dialog) dialog).findViewById(R.id.caixa_dialog_1);
        Esporte s = new Esporte();
        s.setJogadoresFora(caixa_dialog_1.getText().toString());
    }
});
    
13.07.2017 / 22:45