×

Discussion Board

Results 1 to 6 of 6

Hybrid View

  1. #1
    Registered User
    Join Date
    Dec 2012
    Location
    Mumbai, Maharashtra, India
    Posts
    79

    Question How I obtain references of controls to record form's controls state changes??

    In my J2ME app, I want show an alert dialog box only when user change the state of any controls that is user use any of the control of the form & then try to canel the form without saving typed data. For example among all 5-6 controls of form if user type in 1-2 textboxs and try to canel the form without saving that typed data into database.
    But if state of controls is unchanged & user click cancel button then no need to show alert box...
    Any idea how to do this?????

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: How I obtain references of controls to record form's controls state changes??

    Quote Originally Posted by Rahul More View Post
    In my J2ME app, I want show an alert dialog box only when user change the state of any controls that is user use any of the control of the form & then try to canel the form without saving typed data. For example among all 5-6 controls of form if user type in 1-2 textboxs and try to canel the form without saving that typed data into database.
    But if state of controls is unchanged & user click cancel button then no need to show alert box...
    Any idea how to do this?????
    Set class variable - Boolean formChanged=false;

    Put ItemStateListener on the form

    Form.setItemStateListener(ItemStateListener)

    If any item is changed on the form - you will get a call in itemStateChanged(item); here in this method you can set a Boolean formChanged=true; return;

    When the user try to cancel the form - check this formChanged boolean based on it ask for save.


    -- From API refernec ----
    itemStateChanged


    public void itemStateChanged(Item item)
    Called when internal state of an Item has been changed by the user. This happens when the user:
    changes the set of selected values in a ChoiceGroup;
    adjusts the value of an interactive Gauge;
    enters or modifies the value in a TextField;
    enters a new date or time in a DateField; and
    Item.notifyStateChanged() was called on an Item.
    It is up to the device to decide when it considers a new value to have been entered into an Item. For example, implementations of text editing within a TextField vary greatly from device to device.

    In general, it is not expected that the listener will be called after every change is made. However, if an item's value has been changed, the listener will be called to notify the application of the change before it is called for a change on another item, and before a command is delivered to the Form's CommandListener. For implementations that have the concept of an input focus, the listener should be called no later than when the focus moves away from an item whose state has been changed. The listener should be called only if the item's value has actually been changed.

    The listener is not called if the application changes the value of an interactive item.

    Parameters:
    item - the item that was changed
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  3. #3
    Registered User
    Join Date
    Dec 2012
    Location
    Mumbai, Maharashtra, India
    Posts
    79

    Re: How I obtain references of controls to record form's controls state changes??

    I develop this according to your instruction...... but it is not working properly...... suggest me some correction in that....
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;

    public class InformAboutStatechange extends MIDlet implements CommandListener
    {
    Display d;
    Form frm;
    Command Save, Cancel, CancelAlrtYes, CancelAlrtNo, CancelAlrtBack;
    TextField Name, Age;
    Alert CancelAlrt;
    boolean StateChange = false;

    public InformAboutStatechange()
    {
    d = Display.getDisplay(this);
    frm = new Form("");
    Save = new Command("Save", Command.SCREEN, 1);
    Cancel = new Command("Cancel", Command.CANCEL, 2);
    Name = new TextField("Name", "", 10, 0);
    Age = new TextField("Age", "", 10, 0);
    frm.addCommand(Save);
    frm.addCommand(Cancel);
    frm.append(Name);
    frm.append(Age);
    frm.setCommandListener(this);
    d.setCurrent(frm);
    }
    public void startApp()
    {
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
    }

    public void commandAction(Command c, Displayable dispable)
    {

    if(c == Cancel)
    {
    CancelAlrt = new Alert("Application Alert","Save Changes???",null,null);
    CancelAlrtYes = new Command("Yes",Command.SCREEN, 1);
    CancelAlrtNo = new Command("No", Command.SCREEN | Command.CANCEL, 2);
    CancelAlrtBack = new Command("Back", Command.BACK, 3);
    CancelAlrt.addCommand(CancelAlrtYes);
    CancelAlrt.addCommand(CancelAlrtNo);
    CancelAlrt.addCommand(CancelAlrtBack);
    frm.setItemStateListener(new ItemStateListener()
    {
    public void itemStateChanged(Item item)
    {
    item.notifyStateChanged();
    if(item == Name)
    {
    if(item == Age)
    {
    StateChange = true;
    }
    else
    {
    StateChange = false;
    }
    }
    }
    });

    if(StateChange == true)
    {
    d.setCurrent(CancelAlrt);
    CancelAlrt.setCommandListener(new CommandListener()
    {
    public void commandAction(Command cmd, Displayable dispable)
    {
    if(cmd == CancelAlrtYes)
    {
    d.setCurrent(frm);
    }
    else if(cmd == CancelAlrtNo)
    {
    d.setCurrent(frm);
    }
    else if(cmd == CancelAlrtBack)
    {
    d.setCurrent(frm);
    }
    }
    });
    }
    else
    {
    destroyApp(true);
    notifyDestroyed();
    }

    }


    }
    }

  4. #4
    Registered User
    Join Date
    Dec 2012
    Location
    Mumbai, Maharashtra, India
    Posts
    79

    Re: How I obtain references of controls to record form's controls state changes??

    I have develop another code......

    public class InformAboutStatechange extends MIDlet implements CommandListener, ItemStateListener
    {
    Display d;
    Form frm;
    Command Save, Cancel, CancelAlrtYes, CancelAlrtNo, CancelAlrtBack;
    TextField Name, Age;
    Alert CancelAlrt;
    boolean StateChange = false;

    public InformAboutStatechange()
    {
    d = Display.getDisplay(this);
    frm = new Form("");
    Save = new Command("Save", Command.SCREEN, 1);
    Cancel = new Command("Cancel", Command.CANCEL, 2);
    Name = new TextField("Name", "", 10, 0);
    Age = new TextField("Age", "", 10, 0);
    frm.addCommand(Save);
    frm.addCommand(Cancel);
    frm.append(Name);
    frm.append(Age);
    frm.setCommandListener(this);
    frm.setItemStateListener(this);
    d.setCurrent(frm);
    }

    public void startApp()
    {
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
    }

    public void itemStateChanged(Item item)
    {
    item.notifyStateChanged();
    }
    public void notifyStateChanged()
    {
    StateChange = true;
    }

    public void commandAction(Command c, Displayable dispable)
    {
    if(c == Cancel)
    {
    CancelAlrt = new Alert("Application Alert","Save Changes???",null,null);
    CancelAlrtYes = new Command("Yes",Command.SCREEN, 1);
    CancelAlrtNo = new Command("No", Command.SCREEN | Command.CANCEL, 2);
    CancelAlrtBack = new Command("Back", Command.BACK, 3);
    CancelAlrt.addCommand(CancelAlrtYes);
    CancelAlrt.addCommand(CancelAlrtNo);
    CancelAlrt.addCommand(CancelAlrtBack);

    if(StateChange == true)
    {
    d.setCurrent(CancelAlrt);
    CancelAlrt.setCommandListener(new CommandListener()
    {
    public void commandAction(Command cmd, Displayable dispable)
    {
    if(cmd == CancelAlrtYes)
    {
    d.setCurrent(frm);
    }
    else if(cmd == CancelAlrtNo)
    {
    d.setCurrent(frm);
    }
    else if(cmd == CancelAlrtBack)
    {
    d.setCurrent(frm);
    }
    }
    });
    }
    else
    {
    destroyApp(true);
    notifyDestroyed();
    }
    }

    }
    }

    I have tried all things which i can do but still problem persist.........

  5. #5
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: How I obtain references of controls to record form's controls state changes??

    Run the code in debug mode or put some SOP's and check the flow... what is happening... till where it works...or errors you get.

    if(StateChange == true) can be just written as if(StateChange)

    Check the validity of == operator with objects that you are using...
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  6. #6
    Registered User
    Join Date
    Dec 2012
    Location
    Mumbai, Maharashtra, India
    Posts
    79

    Re: How I obtain references of controls to record form's controls state changes??

    Got it.... Now my program working according to my thinking....... once again thanx for helping me Amitabh sir.....

Similar Threads

  1. Getting error when try to add controls in form
    By Basit781 in forum Symbian User Interface
    Replies: 3
    Last Post: 2011-08-04, 16:40
  2. switching between item controls on my form
    By Mrgoos in forum Symbian User Interface
    Replies: 10
    Last Post: 2011-02-03, 07:30
  3. Mixing native controls from UI Design Editor with custom controls
    By elviin in forum Symbian User Interface
    Replies: 1
    Last Post: 2009-05-12, 07:39
  4. Custom controls or extend existing controls?
    By mayur_24 in forum Symbian User Interface
    Replies: 2
    Last Post: 2007-10-05, 16:16
  5. Adding controls to a form dynamically
    By jowin in forum Symbian User Interface
    Replies: 0
    Last Post: 2003-04-26, 02:38

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •