Hi, I want to read a text file and display the text in a single textBox, and I use the code
local file, errorString = io.open( path, "r" ) if not file then print( "File error: " .. errorString ) else for line in file:lines() do if (line ~= nil) then text = text .. "\n" .. line end end newText.text = text io.close( file ) end
Now I want to display a button near each line and, when I press button, the line must be deleted
Ok, now I have a table with my buttons…but on the “onRelease” I can only write a function to call without ()
I have this
local function clearLine() --Remove the selected line end local clearButton = {} for i=1,#lines,1 do clearButton[i] = widget.newButton({ label = "", defaultFile = "clear.png", overFile = "clearOver.png", width = 25, height = 25, onRelease = clearLine }) clearButton[i].y = 300+10\*i end
I want something like “onRelease = clearLine(i)” and thend in the clearLine function I have the refer of “i” so I know wich line remove
local function clearLine(event) --use event.target.id to get your i value end local clearButton = {} for i=1,#lines do clearButton[i] = widget.newButton({ label = "", defaultFile = "clear.png", overFile = "clearOver.png", width = 25, height = 25, onRelease = clearLine }) clearButton[i].id = i clearButton[i].y = 300+10\*i end
In the simulator the app work perfectly…but when I compile it and install on android device, the app can’t load the file and other things don’t work! Why?
You could read the text in, store each line in a table (indexed by a number/array). Then when you hit the delete button, delete the object from the display, delete the entry from the table (table.remove( tableName, indexToRemove ) ). Then loop over the remaining table and write the file back out.
Yes, I used a table to manage the file lines and all work correctly…
On the device looks like if the app can’t load the file in sandbox/document…maybe I have to change some setting in the loading file code? Or maybe the devide doesn’t have a sandbox :huh:
You have three (well four) places to store files and they have specific uses:
system.DocumentsDirectory – gets backed up, not used for temporary files or files that can be downloaded from the Internet.
system.CachesDirectory – where you should store files that can be downloaded from the Internet. Can be deleted when storage is low.
system.TemporaryDirectory – where you should store throw away files. Can be deleted at any time by the device.
system.ApplicationSupportDirectory – A place to store files that need to persist between sessions like system.DocumentsDirectory these files don’t show up to users who use apps like iTunes to look at the Documents directory.
Each of these can have sub-directories as part of their path.
In this case the app need the file like “setting” to work (the setting are editable by the user), so I put it in my pc folder C:\Users\AppData\Local\Corona Labs\Corona Simulator\Sandbox\myproject\Documents
When I build and install the app, the file don’t load
When you install the app on device the documents folder will be empty. You will need to include a default settings file in your app and copy it to documents folder when your app first opens so it is user editable.