Read userInput & store in file & display on screen

Good day:

a )  want to read userInput from screen (text inputs - single line mostly)

b )  store or update file (text file)

c )  display the value in another location on the screen

I have looked at the tutorials, guides, etc. etc. and have not been able to locate the info on the proceedure

I am trying to get working.  One issue (general one) is that most examples & tuts examples are not clear as to what must be literal and what is replaced w/ some “real” value.

If you can help, thank you.  This is my first app.

You should try to read documentation
1)https://docs.coronalabs.com/api/library/native/newTextField.html
2)https://docs.coronalabs.com/daily/guide/data/readWriteFiles/index.html
3)https://docs.coronalabs.com/api/library/display/newText.html
I always thought I could get away with not reading, but it is important tool you have to master.

Thanks for the suggestions.  Unfortunately, I have already looked at the links you show.  The info falls short.  It is not clear how to get a text input and do something w/ it.  Yes, I am new to LUA.  This issue has been under investigation for many many days.

I much appreciate all of your help.

Ed made this sample a while back when I was having trouble with text boxes which maybe of some help

Edit: forgot link
https://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd/2016/04/native_textbox

If you are new to Lua check out this
https://coronalabs.com/learn-lua/

Thank you.   I have been thru all of the videos, tutorials, etc. at “learn lua”.  See post re: confusion w/ unclear “examples”.

Believe me, I have been thru ALL of the available education at learn lua & looked at many commands, but still not clear in accepting input from a text box & printing it or saving it to a file (basically, I am unclear how a quantity from a text input box is captured and then processed (stored, printed, etc.).  How does the text box know when user has finished input and stores into a variable.  On a PC, there is a return or a button to execute the capture.  Not that way on phones.

I appreciate all of your help and suggestions.  I will check the example you linked.  I will revisit the “learn lua” again.

I mistakenly thought “learn lua” was the menu under “learn” on the main page.  Thanks again.

I will try to break down text boxes and text field the best I can. First they are native which means they will alway be on top of any thing else (expected for other native things). Other than that they really operate like other display objects. They can be inserted into groups, you can use display.remove() to delete them. Thier are some exceptions Windows text box backgrounds can not be removed, basically what you see is what you get on Windows. Also Windows phone does not have native elements. You can add event listeners which basically like corona tell you, yo something changed and you can do what you will with it. Event listeners are only activate when something is changed or updated. In order to get text from a box or field you would call .text

for example if you have a text field called myTextFeild

print(myTextFeild.text)

This would give you text input you want. But if you read the examples in the documentation above it will show how to take advantage of event listeners( it is link 1)

as for the return button, I don’t know what you are talking about but iPhones and most android phones (that I know of) have some form of return button, it could say go, enter, or search. As for how you would get this input the text listener event.phase == “submitted”. If user click anywhere but your text box or field it should return event.phase == “ended”

THANKS!!!

That was very informative.  I think I understand the mechanics better now.  I will continue to look over the info you provided.

I was talking about the <return> on the PC keyboard.

MUch appreciated.

The return key is handled as “submitted” event.phase for boxes and fields for pcs

OK. thx.

Our business app sample (https://github.com/coronalabs-samples/business-app-sample) has a page that shows how to read data from an SQLite database, populate a tableView. Tap on one of the items and go to a form that lets you edit the data and save it and has a button to add new records.

It’s a great resource on how to figure out how to do text field work in Corona. Also don’t forget to look in our sample code that ships with the product. Its in your CoronaSDK/SampleCode/Interface folder.

Rob

this is part of my code; I get this below; any ideas?

"   userName1  =   " {_class = {__index = nil, addEventListener = function() --[[…skipped…]] end, removeEventListener …

addEventListener: listener cannot be nil: nil

stack traceback:

[C]: in function ‘error’

?: in function ‘getOrCreateTable’

?: in function ‘addEventListener’

?: in function ‘addEventListener’

F:\app00000006a\MPGcalcA\main.lua:44: in main chunk

------------- code 

local userName1     = native.newTextField(360,-85.33333333333328,690,34.13333333333333)

print( "   userName1  =   ", userName1 )

–  local userName1     = native.newTextField( dispCentX, (  dispTop - ( spaceInputFieldY * posuserName1Y     ) ), ( dispWd - 30 ), sizeInputFieldY )

userName1:addEventListener( “userInput”, readSnapData )

where is your readSnapData function?

this is it:

–  

local function readSnapData( event )  –  chg ( userInput ) to ( event )?

pathSnapAOpenRead, errorStringpathSnapAOpenRead = io.open( pathSnapA , “r” )  –  open for read

if not pathSnapAOpenRead then

–  Error; output cause

print( "File error: " … errorStringpathSnapAOpenRead )  –  DEBUGmrp

else

–  Grab lines & stick in to table

index = 0  –  start index at zero then increment inside loop to correct start value (1)

for line in pathSnapAOpenRead:lines() do

if line then

   index = index + 1  –  increment to get correct table index

   snapTable[“index”] = line

else

   print( "data not found in file pathSnapA  ", pathSnapA )

print( "capture new user inputs to populate snap-shot file   ", pathSnapA )

end

end

–  Close the file – move to end of processing; final tasks section

–  io.close( pathSnapAOpenRead )  – close file / handle

–   return true

end

–  put at end w/ final tasks    pathSnapAOpenRead = nil  –  release memory allocation for file handle

end

–  --------------------------------------------------------------------------

try this:

[lua]

–  

local function readSnapData( event )  –  chg ( userInput ) to ( event )?

pathSnapAOpenRead, errorStringpathSnapAOpenRead = io.open( pathSnapA , “r” )  –  open for read

if not pathSnapAOpenRead then

–  Error; output cause

print( "File error: " … errorStringpathSnapAOpenRead )  –  DEBUGmrp

else

–  Grab lines & stick in to table

index = 0  –  start index at zero then increment inside loop to correct start value (1)

for line in pathSnapAOpenRead:lines() do

if line then

   index = index + 1  –  increment to get correct table index

   snapTable[“index”] = line

else

   print( "data not found in file pathSnapA  ", pathSnapA )

print( "capture new user inputs to populate snap-shot file   ", pathSnapA )

end

end

–  Close the file – move to end of processing; final tasks section

–  io.close( pathSnapAOpenRead )  – close file / handle

–   return true

end

–  put at end w/ final tasks    pathSnapAOpenRead = nil  –  release memory allocation for file handle

end

–  --------------------------------------------------------------------------

[/lua]

continuing with this, from the sample code/storage/saveTable/main.lua file, in the function loadData(), it calls out a library str that is not found by the simulator.  it doesn’t look like a typo for string.  it is used to split the line up & reformat it.

my goal is to take a table, store in a file, retrieve it later and put into the original table it came from via an intermediate table;

Where is the ‘str’ library & how do I get it ready to use?

Thanks.


[lua]

local str = require(“str”)  –  LIBRARY CAN NOT BE LOCATED

if file then

– Read file contents into a string

local dataStr = file:read( “*a” )

– Break string into separate variables and construct new table from resulting data

local datavars = str.split(dataStr, “,”)

dataTableNew = {}

for i = 1, #datavars do

– split each name/value pair

local onevalue = str.split(datavars[i], “=”)

dataTableNew[onevalue[1]] = onevalue[2]

end

io.close( file ) – important!

– Note: all values arrive as strings; cast to numbers where numbers are expected

dataTableNew[“numValue”] = tonumber(dataTableNew[“numValue”])

dataTableNew[“randomValue”] = tonumber(dataTableNew[“randomValue”])

else

print (“no file found”)

end

[/lua]

Where did you get this code?  The only reference to str after the require is a “str.split()”. The standard Lua string library does not have a split function. The split function is an easy to break a long string into many short strings based on some divider. In the example above it’s dividing on a comma.

There are plenty of versions of a split function that can be found online. The one I use is this:

function string:split( inSplitPattern, outResults ) if not outResults then outResults = { } end local theStart = 1 local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) while theSplitStart do table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) ) theStart = theSplitEnd + 1 theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) end table.insert( outResults, string.sub( self, theStart ) ) return outResults end

My version attaches it to the string library namespace, so instead of str.split, it would be string.split. Drop that into your code where you’re trying to require the str library and change the use of it later to string.split instead of str.split.

Rob

Rob

Thank you for responding.  I changed the require & the commands str to string.  There is still a problem.

here is the setup:

local string = require(“string”)

command (one example)

local datavars = string.split(dataStr, “,”)  –  ? will this work w/ fields having ‘,’ in them?

From OUTPUT window:

“F:\app00000006a\MPGcalcA\main.lua:201: attempt to call field ‘split’ (a nil value)\nstack traceback:\n F:\app00000006a\MPGcalcA\main.lua:201: in function <F:\app00000006a\MPGcalcA\main.lua:170>\n ?: in function <?:205>”

F:\app00000006a\MPGcalcA\main.lua:201: attempt to call field ‘split’ (a nil value)

stack traceback:

F:\app00000006a\MPGcalcA\main.lua:201: in function <F:\app00000006a\MPGcalcA\main.lua:170>

?: in function <?:205>

Debugging session completed (traced 0 instructions).

You don’t need to require(“string”). You just need to copy/paste that function I provided instead of doing:

str = require(“str”)

Rob