I'm trying to implement a method whose purpose is to get confirmation from a user before proceeding with certain potentially destructive actions. What I'm after is a method that puts up an Alarm with yes/no choices and returns the selection to whatever section of code called it. The relevant code sections are as follows:
<CODE>
// In the main MIDlet
//
public void commandAction(Command c, Displayable d) {
String s = "";
boolean b = true;
char k;
kommand = c.getLabel();
//
// ... handlers for other commands
//
if (c == CMD_DELETE) {
if (current_name != "/") {
kommand = "Delete";
k = getConfirmation();
/****** DON'T DELETE ANYTHING YET!!!!!!
try {
current_fc.setFileConnection(s);
current_fc.dele;te();
current_fc.setFileConnection("..");
} catch (Exception e) {}
****** We need to write the confirmation getter first! ****/;
}
showDirs();
}
} // End of command actions
// The confirmation getter
// I've also tried declaring this method as a boolean
//
public char getConfirmation() {
char k = '?';
Alert ExitAlrt = new Alert("Confirmation Required",
"Are you sure?", null, AlertType.WARNING);
ExitAlrt.addCommand(CMD_YES);
ExitAlrt.addCommand(CMD_NO);
ExitAlrt.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c == CMD_YES) {
kommand = kommand + "Y";
} else {
kommand = kommand + "N";
}
tick.setString(kommand);
mainDisplay.setCurrent(flist);
notify();
}
} );
// This section of code results in generating an extra class
// file with the same name as the main class file but with a
// $1 appended.
mainDisplay.setCurrent(ExitAlrt);
try {
wait();
} catch (InterruptedException ie) {}
k = kommand.charAt(kommand.length()-1);
return k;
}
</CODE>
This is the version that gets me the closest results to what I want. Still, from what I've been able to determine from what shows up in the ticker and when, program execution is not returning to the position from which getConfirmation() was called. Instead it seems to be skipping the rest of the Delete command's if block and going back to the main CommandListener.
I originally tried it the same as I would have done it in C: without the notify() and try-wait-catch. That version flashed the alert on the screen for on the order of a tenth of a second then went back to the main display. Adding the try-wait-catch allowed the Alert to stay on the screen, but the display wouldn't switch back when I made a selection. Adding the notify() allowed what looks at first glance like the desired behavior except that what should show up in the ticker doesn't. (The showDirs() method calls another method which puts other information in the ticker.)
Exactly how is this supposed to be done?