Display a newobject for each line in file

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

How can I do this?

Use widget library : https://docs.coronalabs.com/api/library/widget/newButton.html

local function suppress(event) --select your line here to do what you want end -- local button1 = widget.newButton(     {         label = "button",         onRelease = suppress,         emboss = false,         shape = "roundedRect",         width = 100,         height = 100,         cornerRadius = 5,         fillColor = { default={1,0,0,1}, over={1,0.1,0.7,0.4} },         strokeColor = { default={1,0.4,0,1}, over={0.8,0.8,1,1} },         strokeWidth = 4     } )

And add button1 near the textBox at y’s coordonnate of the lines.

With this code I display one button, I want more button (one for each line, maybe 3, maybe 10 depends by the file)

I think that I must use tables, but I don’t know how

Look into using a tableView

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

:wub:  thank you!!

Last question  :smiley:

Now if I want to remove the specific line from a file?

I tried to read the file --> store the content in a variable --> write the file without one line

But I think that there is an easy way

if you are using text files? No

If you want to manipulate like that then I would advise using a local database like SQL Lite

Ok, thank you! I have a lot of documents and guides to read  :smiley:

p.s. the app is growing up 

I have a problem!

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?

Devices are case sensitive, Windows isn’t.  This is such a common cause of errors.

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.

Rob

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.

Rob

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.

Ok, but how can I write a file with the starting setting without overwrite it whenever I open the app?

–Edit: Dumb question  :lol: , I simply check if the file already exist! If not mean that is the first time

Use widget library : https://docs.coronalabs.com/api/library/widget/newButton.html

local function suppress(event) --select your line here to do what you want end -- local button1 = widget.newButton(     {         label = "button",         onRelease = suppress,         emboss = false,         shape = "roundedRect",         width = 100,         height = 100,         cornerRadius = 5,         fillColor = { default={1,0,0,1}, over={1,0.1,0.7,0.4} },         strokeColor = { default={1,0.4,0,1}, over={0.8,0.8,1,1} },         strokeWidth = 4     } )

And add button1 near the textBox at y’s coordonnate of the lines.

With this code I display one button, I want more button (one for each line, maybe 3, maybe 10 depends by the file)

I think that I must use tables, but I don’t know how