In J2ME, How I obtain reference of all controls of the form to record form's controls state changes?

五迷三道 提交于 2019-12-07 20:46:28

Your mistake is invoking setItemStateListener too late.

In your code snippet, listener is set in commandAction method, which in turn is invoked only when user presses command button. At this moment, it is just too late to start "listening" and all the changes you'd want to track are already lost.

For your purposes, item state listener should be set prior to displaying form (ie prior to invoking d.setCurrent), as in the example below:

    // ...
    frm.setCommandListener(this);
    // ---> set item state listener here
    frm.setItemStateListener(new ItemStateListener() 
    {
      public void itemStateChanged(Item item) 
      {
        item.notifyStateChanged();
        if (item == Name) {
          if (item == Age) {
            StateChange = true;
          } else {
            StateChange = false;
          }
        }
      }
    });
    d.setCurrent(frm);
    // ...

I would also move code from InformAboutStatechange constructor into startApp method, as explained for example in tutorial MIDlet Life Cycle -> Execution States:

The constructor typically does little or no initialization... Typically, you'll use startApp() to allocate record stores, network connections, UI components, and such...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!