Why isn't my commandListener working?

83 Views Asked by At

I'm learning to develop some basic applications for Nokia S40 devices. I've created a couple of Commands for the user to use. The problem is that command 'no' works however 'yes' does nothing. Why is this? The example is as follows:

    final Form form = new Form("Form");
    form.append("Update?");

    final Command yes = new Command("Yes", Command.OK, 1);
    final Command no = new Command("No", Command.CANCEL, 1);

    form.addCommand(yes);
    form.addCommand(no);
    form.setCommandListener(new CommandListener() {

        public void commandAction(Command arg0, Displayable arg1) {
            // TODO Auto-generated method stub

            if (arg0 == yes) {
                System.out.println("Yes pressed");
            } else if (arg0 == no) {
                System.out.println("No Pressed");
            }
        }
    });

Any pointers or advice would be appreciated.

1

There are 1 best solutions below

0
mr_lou On

You cannot compare two objects using the equal sign.

You have to use the Object.equals(anotherObject) method.

So replace if (arg0 == yes) with if (arg0.equals(yes))

and if (arg0 == no) with if (arg0.equals(no)).