Вопрос

I'm working with java me, I built an app using forms displayables. I'm trying to switch to other forms, based on the user's input in a textfield item. For example, I want the user to be able to type in the number "1" in the textfield and then be taken to form1 or type in "2" and be taken to form2 etc.

What's the code to do this?

Here's what I did but it's not working as expected:

form.setItemStateListener(new ItemStateListener() {


            public void itemStateChanged(Item item) {
                if (item == TextField) {
                     if ("1".equals(TextField.getString())) {
                        switchDisplayable(null, form1);
              }   
           }   
}

I've done as adviced. I added a command to the textfield item and listen on it to read textfield contents and then compare the contents as a string, to switch forms. See my code below, still not working. I think maybe there's something I'm missing or my logic is not right.

        form.setCommandListener(new CommandListener() {

            public void commandAction(Command command, Displayable displayable) {
                if (command == getTextFieldItemCommand()) {
                    if ("1".equals(TextField.getString())) {
                        switchDisplayable(null, form1);
                    } else if ("2".equals(TextField.getString())){
                        switchDisplayable(null, form2);
                    }
                }
            }
Это было полезно?

Решение

It looks like you expect method itemStateChanged to be invoked when it feels convenient to you, like at every character entry in the text field.

Above expectation is wrong, specified behavior is explained in API javadocs:

It is up to the device to decide when it considers a new value to have been entered into an Item... In general, it is not expected that the listener will be called after every change is made...

Given above, using itemStateChanged the way you want makes very little sense, consider changing design of your MIDlet.


I for one would probably just add a command Go and command listener to the form or text field and read text field contents when user invokes that command to find out which displayable they want to switch to.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top