0

In my application I have a point where I need to save a customer.

I do some checks to verify if every field has a value and then i need to prompt a dialog if someone is blank, send back a boolean value and, based on its value save or not the customer.

Here is my code:

private boolean canBeSaved(ArrayList<View> viewList) {
    if (!viewList.isEmpty()) {
        showErrorDialog();
        for (View v : viewList) {
            ((EditText) v).setError("errorMsg");
            v.requestFocus();
        }
        return false;
    } else {
        return true;
    }
}

private void showErrorDialog() {
    QustomDialogBuilder saveDialog = new QustomDialogBuilder(this);
    saveDialog.setTitle("ATTENZIONE!");
    saveDialog.setMessage(Constants.CANT_SAVE);
    saveDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    saveDialog.show();
}

but after executing showErrorDialog(); I cannot wait onClick event to resume the flow.

The flow I got at the moment is: showErrorDialog() -> dialog.show() -> rest of canBeSaved(..)

The flow I want is: showErrorDialog() -> dialog.show() -> wait onClickEvent -> rest of canBeSaved(..)

Is this possible or I'm asking the moon? I'm doing everything wrong?

Luca
  • 823
  • 4
  • 11
  • 31
  • 1
    A modal dialog like that is not the Android way. Consider changing your design so that you can fire up a listener that does what you want from the dialog `onClick()`. http://stackoverflow.com/questions/2028697/dialogs-alertdialogs-how-to-block-execution-while-dialog-is-up-net-style – laalto Sep 16 '14 at 13:15
  • Thanks everyone for your solution... But I think I'm a coward: I've solved everything showing only a Toast... – Luca Sep 16 '14 at 13:53

2 Answers2

1

I'm not all that familiar with the DialogInterface but I believe what you want to do is:

private void showErrorDialog() {
final QustomDialogBuilder saveDialog = new QustomDialogBuilder(this); //add final
saveDialog.setTitle("ATTENZIONE!");
saveDialog.setMessage(Constants.CANT_SAVE);
saveDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        saveDialog.dismiss(); //change to saveDialog
    }
});
saveDialog.show();
}
RyPope
  • 2,645
  • 27
  • 51
1
private boolean canBeSaved(ArrayList<View> viewList) {
    if (!viewList.isEmpty()) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("ATTENZIONE!")
           .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // Handle Ok
                       for (View v : viewList) {
                            ((EditText) v).setError("errorMsg");
                            v.requestFocus();
                        }
                        saveDialog.dismiss();
                        return false;
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // Handle Cancel
                   saveDialog.dismiss();
                   return false;
               }
           }).create();

    } else {
        return true;
    }
}
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77