Saving indexed data in tableview using GGData

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. 

JSON, and thus GGData, only stores values in strings so you can’t index them when they are still in storage.

You’ll need to do something like this:
 

local checked = settings:get( "checked" ) local isChecked = checked[row.index]

Or even shorter:
 

local isChecked = settings:get( "checked" )[row.index]

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]  

Still the same error  :( 
My code:

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

Sorry that was my mistake, serves me right for replying before coffee.

You’ll need to put the ‘checked’ table back into GGData after you’ve edited the value in your onPress function like this:

settings:set( "checked", checked ) settings:save()

Finally! :D 
It’s working. Thanks!
Keep on making great libraries and apps!

Glad it’s working and we’re sure going to try  :slight_smile:

i have another question
My box looks like this
 

 local box = widget.newSwitch { style = "checkbox", --id = "Checkbox button", onPress = onSwitchPress, initialSwitchState = false or settings:get( "checked" )[row.index] }

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:

initialSwitchState = settings:get( "checked" )[row.index]

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:

initialSwitchState = settings:get( "checked" )[row.index] or false

Hope that helps!

initialSwitchState = settings:get( "checked" )[row.index] or false

Got the same error.
Luckily, I have already overcome the problem with this:
 

local switchState if settings:get( "checked" ) == nil then switchState = false else switchState = settings:get( "checked" )[row.index] end local box = widget.newSwitch { style = "checkbox", --id = "Checkbox button", onPress = onSwitchPress, initialSwitchState = switchState }

Glad you got it working!

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. 

JSON, and thus GGData, only stores values in strings so you can’t index them when they are still in storage.

You’ll need to do something like this:
 

local checked = settings:get( "checked" ) local isChecked = checked[row.index]

Or even shorter:
 

local isChecked = settings:get( "checked" )[row.index]

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]  

Still the same error  :( 
My code:

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