Hello everyone
I am trying to manage over 100 checkboxes in my app without creating individual blocks for each one. I’m using the “Any Component” blocks along with a Global List and TinyDB to save and restore the checked states
Here is my current logic (as shown in the attached image):
Saving: When Any Checkbox.Changed, I check if the component is checked. If true, I add the component to a global list. If false, I remove it. Then I store the entire list in TinyDB using the tag “one”.
Loading: On Screen.Initialize, I retrieve the list from TinyDB and use a for each item in list loop to set the Checked property of each item to true.
The Problems I’m facing:
The checked states are not being saved or restored correctly.
I sometimes get a Runtime Error: “Property setter was expecting a CheckBox component but got a String”.
It seems like TinyDB might be converting my component objects into strings when saving, which breaks the set Checkbox.Checked block during initialization.
What is the best way to fix this? Should I store the component’s name/text instead of the component object itself? If so, how can I efficiently find and check the boxes again during Screen Initialize?
When the app opens, all the checkboxes are empty I meant that when it opens it should automatically check the ones that were previously selected and read the items based on those.
yes it will happen because the name of the check box will change eveytime when the screen is initialised. As per your block it seems if the user selectes ny check boxe and later when he open the checked box should be marked, again if he uncheck and open again it have to behave the same. To achieve this simple use index based method like this
Since the names of the checkboxes are generated dynamically you cannot set the status of saved checkboxes all the time from the tinydb into exisitng checkboxes.
the problme is , while he selecting a checkbox he get the componet as com.google.appinventor.components.runtime.CheckBox@72555fb but next time when he initialise the screen he get the same checkbox name as com.google.appinventor.components.runtime.CheckBox@35de45. But in the for each item list component was taken the saved component name as String and so he got that error
Maybe it would be better to create them dynamically
Edit:
Try this, it’s similar to what Still-learning suggested but not identical.
I also recommend using CheckBox.Click because the Changed event is triggered even when the screen is initialized, causing the data to be saved again unnecessarily.