WIngenia WIngenia - 2 months ago 16
Java Question

Execute after close dialog

I have a button that open a dialog for change a number. The button is in activity, and generate a new class names dialogs, for storage different dialogs.

Dialog consigna: Dialogs.class

public String consigna(){


AlertDialog.Builder alert = new AlertDialog.Builder(ctxt);
alert.setTitle("Nueva temperatura");
alert.setMessage("Agrega una nueva temperatura");
final EditText input = new EditText(ctxt);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
data = input.getText().toString();

}
});
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Put actions for CANCEL button here, or leave in blank
}
});
alert.show();

return data;
}


on click button: MainActivity

//I need change these two values:

final public double temperatura = 200.3;
String newData;

...

public void onClick(View v) {

switch (v.getId()){

case R.id.tempConsigna:
dialog = new Dialogs(MainActivity.this);
String data = dialog.consigna();
newData = data;
break;

...//other cases...


The issue is in newData = data; I doesn't have a data, because the dialog not are closed. The dialog work in other thread,no?

How to change newData var with the dialog result? It is posible into a dialogs class?

Answer
    you need a callback so implement an interface for it. 

in Dialogs class
      public String consigna(final OnConfirm confirm){
            final String[] data = new String[1];
            AlertDialog.Builder alert = new AlertDialog.Builder(activity);
            alert.setTitle("Nueva temperatura");
            alert.setMessage("Agrega una nueva temperatura");
            final EditText input = new EditText(activity);
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
            alert.setView(input);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                     data[0] = input.getText().toString();
                    confirm.onConfirm(data[0]);
                }
            });
            alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //Put actions for CANCEL button here, or leave in blank
                }
            });
            alert.show();

            return data[0];
        }


 public interface OnConfirm {
         void onConfirm(String s);
    }


in Main Activity 

Dialogs dialog = new Dialogs(MainActivity.this);
        dialog.consigna(new Dialogs.OnConfirm() {
            @Override
            public void onConfirm(String s) {

                Log.d("data", s);
            }
        });
Comments