Help: Checkboxes not triggering listener

Hello guys, I have a list of 7 checkboxes that when the user clicks on any of them, it would run a function to update the content of a table that holds 7 elements.  However I have trouble getting it to work.  Currently, clicking on the checkboxes seems to have no effect on the table.  Am I doing anything wrong here?

[lua]

 --Define weekday table local vacSchedule = {1,1,1,1,1,1,1} --Create weekday checkboxes local monSwitch = widget.newSwitch{style = "checkbox",id = "2",initialSwitchState = true} local tueSwitch = widget.newSwitch{style = "checkbox",id = "3",initialSwitchState = true} local wedSwitch = widget.newSwitch{style = "checkbox",id = "4",initialSwitchState = true} local thuSwitch = widget.newSwitch{style = "checkbox",id = "5",initialSwitchState = true} local friSwitch = widget.newSwitch{style = "checkbox",id = "6",initialSwitchState = true} local satSwitch = widget.newSwitch{style = "checkbox",id = "7",initialSwitchState = true} local sunSwitch = widget.newSwitch{style = "checkbox",id = "1",initialSwitchState = true} --Place checkboxes into table weekdaySwitch = {sunSwitch, monSwitch,tueSwitch,wedSwitch,thuSwitch,friSwitch,satSwitch} --When checkboxes are clicked on/off, update the weekday table accordingly local function checkboxSwitchListener( event ) local switch = event.target if switch.isOn == true then vacSchedule[tonumber(switch.id)] = 1 else vacSchedule[tonumber(switch.id)] = 0 end end --Attach listener to check for clicking of the checkboxes for cycleDay = 1, 7 do weekdaySwitch[cycleDay].onPress = checkboxSwitchListener end 

[/lua]

I think you need to assign the listener at creation time, so…

  1. Move your listener definition above the creations

  2. Include the listener onPress assignment in your creation calls.

Thank you RoamingGamer, I will give your suggestion a try when I get home.  Many thanks again!

Ed is right. As a rule with our widget library, you cannot access creator values outside of the creation process:

local switch = widget.newSwitch( { onPress = something } )

causes a table that’s internally called options to be passed in. Internally we read that options table and setup private variables that are not intended to be accessed later like maybe:

local _onPress = options.onPress

These are not exposed to the entire object. Later when you do:  switch.onRelease = something, you’re adding a new member variable to the switch object, but the code’s not going to look for that, it’s going to look for the local _onPress variable (or whatever it’s called).

Rob

I think you need to assign the listener at creation time, so…

  1. Move your listener definition above the creations

  2. Include the listener onPress assignment in your creation calls.

Thank you RoamingGamer, I will give your suggestion a try when I get home.  Many thanks again!

Ed is right. As a rule with our widget library, you cannot access creator values outside of the creation process:

local switch = widget.newSwitch( { onPress = something } )

causes a table that’s internally called options to be passed in. Internally we read that options table and setup private variables that are not intended to be accessed later like maybe:

local _onPress = options.onPress

These are not exposed to the entire object. Later when you do:  switch.onRelease = something, you’re adding a new member variable to the switch object, but the code’s not going to look for that, it’s going to look for the local _onPress variable (or whatever it’s called).

Rob