Hey guys, I’m buliding a checklist using tableview and checkbox widgets in my app. Apparently I need to save the checkbox value. I’m currently using GGData. I’m trying to use row.index to save and load value for each row, but it doesn’t work.
I’ll appreciate any help or sample checklist code. Thanks!
My code:
local list local checked = {} -- Handle row rendering local function onRowRender( event ) local phase = event.phase local row = event.row -- Load or Create settings box settings = GGData:new( "settings" ) settings:get( "checked{}") settings:save() --- if checkbox is touched local function onSwitchPress () settings:set("checked[row.index]", true) settings:save() end --onswitchpress local box = widget.newSwitch { style = "checkbox", onPress = onSwitchPress, initialSwitchState = settings:get("checked[row.index]") } end --onrowrender
I’m replying on my phone so sorry for the short reply but in your call to GGData:get() you have ‘{}’ in the string, I’m not 100% but I’m assuming that GGData may not like that.
Thanks for your reply. I’ve changed it to settings:get(“checked[row.index]”), but still it doesn’t work. As far as I can see, indexing doesn’t work, so checked[row.index] is considered as a seperate value and row.index can’t be substituted by row numbers.
i get “an attempt to index local “checked”(boolean value)” error while I’m trying to do it.
Maybe there is a way to save the value and save the row.index seperately and then match them. Though, I can’t figure out how to do it now.
Sorry I thought that the “checked” value was a table as you were trying to index it like one. You can use GGData to store both the value and the index if you like.
I would store the values something like this:
-- Inside the onPress function local checked = settings:get( "checked" ) or {} checked[row.index] = true
Then you can find out if a row is checked like this:
local isChecked = settings:get( "checked" )[index]
local function onSwitchPress () local checked = settings:get( "checked" ) or {} checked[row.index] = true settings:save() local isChecked = settings:get( "checked" )[index] print(isChecked) end --onswitchpress
When i start my app for the first time I get “attempt to index a nil value” error. If I replace initialSwitchState field to just “false” the app starts normally. When I set some value for “checked” I can then replace it with “initialSwitchState = false or settings:get( “checked” )[row.index]” and it works okay.
You are essentially using what is called a ternary operator there with this line:
initialSwitchState = false or settings:get( "checked" )[row.index]
What that is saying is set the ‘initialSwitchState’ value to ‘false’ if ‘false’ is equal to ‘true’ ( which it isn’t ) or set it to the value in the GGData box. As you have it now it is the same as this:
And it’s failing as it’s trying to set it to nil until you set that GGData value with something.
I’m assuming you are trying to set the state of the checkbox to the value in the GGData box and if that isn’t set then set it to false? ( which is strange as I was doing that exact thing today as I was putting a settings screen in our new game ). If so you want to change it to this:
I’m replying on my phone so sorry for the short reply but in your call to GGData:get() you have ‘{}’ in the string, I’m not 100% but I’m assuming that GGData may not like that.
Thanks for your reply. I’ve changed it to settings:get(“checked[row.index]”), but still it doesn’t work. As far as I can see, indexing doesn’t work, so checked[row.index] is considered as a seperate value and row.index can’t be substituted by row numbers.
i get “an attempt to index local “checked”(boolean value)” error while I’m trying to do it.
Maybe there is a way to save the value and save the row.index seperately and then match them. Though, I can’t figure out how to do it now.
Sorry I thought that the “checked” value was a table as you were trying to index it like one. You can use GGData to store both the value and the index if you like.
I would store the values something like this:
-- Inside the onPress function local checked = settings:get( "checked" ) or {} checked[row.index] = true
Then you can find out if a row is checked like this:
local isChecked = settings:get( "checked" )[index]
local function onSwitchPress () local checked = settings:get( "checked" ) or {} checked[row.index] = true settings:save() local isChecked = settings:get( "checked" )[index] print(isChecked) end --onswitchpress