0
votes

How can I get a value from the codenameone OnOff switch state. I have searched all over the internet and I cant find an example.I want to know when its on or off. Please help. I have tried this and many other combinations but I have not got a way yet. I have also read through codename one developers manual, there is no guidance on that.

protected void onMain_AccidentCoverAction(Component c, ActionEvent event) {

   String state = findAccidentCover(c).getOn();

   Dialog.show("State", state, "OK", null);

}

I have also tried

protected void onMain_AccidentCoverAction(Component c, ActionEvent event) {

   String state = findAccidentCover(c).getComponentState().toString();

   Dialog.show("State", state, "OK", null);

}
1
Rather then searching you should try to code and if strucked paste the code and we would be more then happy to help - sol4me
I have tried many combinations but they all never seemed to work, so din't really see the point of writing a lot of code that doesn't work anyway to prove my point...that I need help at this point. Thanks for answering though. - Caleb Nasio
I see your comment in reply to my question, so I think what is missing is a description of exactly why things didn't work - didn't compile, crashed, always returned false, or what? Does findAccidentCover actually return the correct OnOffSwitch? - Ken Y-N
I just need the switch to trigger an action when I either switch it on or off. That's all I need/am trying to do. - Caleb Nasio

1 Answers

1
votes

Googling, I found this page, so it would appear your solution might be:

protected void onMain_AccidentCoverAction(Component c, ActionEvent event) {
   boolean state = findAccidentCover(c).isValue();

   Dialog.show("State", state ? "On" : "Off", "OK", null);
}

As an aside, isValue() is a terrible name for the method.