[Resolved] Save textfields and display

Hello Corona users,

Can someone give me some advise or sample code for my problem.

I have a App that got 2 screens ( 2 files , A.lua and B.lua)
In screen A there is a button, if the button is touched then screen B will show. And in Screen B there is a back button. If the back button is touched then screen A will show. That’s all working good :slight_smile:

But the problem…I want 12 textfields in screen B. And if the user input there information in the different textfields and touched the back button. The user information (that the user input in screen B) must be displayed at screen A.

And the information must be stored, because if the user is back at screen B. The input information must in the textfields.

What is the best and (if possible, easy way) to to this? I have already take a look at SQLite, JSON and ICE but find it hard to accomplish this.

Hope someone can help me :slight_smile:

[import]uid: 150924 topic_id: 27333 reply_id: 327333[/import]

Hey there,

If you’re looking for the easiest way I’d actually recommend Ego - it’s nowhere near as powerful or flexible as other options but it is easier and will get the job done. You can find it on Techority here; http://corona.techority.com/2011/12/28/ego-easy-saving-and-loading-in-your-corona-apps/

You’d simply save yourField.text to a file this way and could read it back when needed.

Peach :slight_smile: [import]uid: 52491 topic_id: 27333 reply_id: 111130[/import]

Peach, thank you for the Ego library!

I’m working on the sample code, to understand/learn the Ego library :slight_smile:
So far I got a second score to work :slight_smile: But the textfield is not working… The input text will not be saved in the text file.

I think the problem is:

-- Add defaultField local defaultField = ""

The full code:

[code]–Hide the status bar
display.setStatusBar(display.HiddenStatusBar)

–Saving/Loading Stuff
local ego = require “ego”
local saveFile = ego.saveFile
local loadFile = ego.loadFile

–Create the background and make it blue
local bg = display.newRect( 0, 0, 320, 480 )
bg:setFillColor( 150, 180, 200 )

–Start the score at 1
local score = 1

– Add score2
local score2 = 2

–Create score text and make it dark gray
local scoreText = display.newText(score, 200, 20, native.systemFont, 24)
scoreText:setTextColor( 80, 80, 80 )

– Add score2
–Create score text and make it dark gray
local score2Text = display.newText(score, 200, 20, native.systemFont, 24)
scoreText:setTextColor( 80, 80, 80 )
– Add defaultField
local defaultField = “”-- forward reference (needed for Lua closure)

– TextField Listener
local function fieldHandler( getObj )

– Use Lua closure in order to access the TextField object

return function( event )

print( "TextField Object is: " … tostring( getObj() ) )

if ( “began” == event.phase ) then
– This is the “keyboard has appeared” event

elseif ( “ended” == event.phase ) then
– This event is called when the user stops editing a field:
– for example, when they touch a different field or keyboard focus goes away

print( "Text entered = " … tostring( getObj().text ) ) – display the text entered

elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key
– (if available) on the onscreen keyboard

– Hide keyboard
native.setKeyboardFocus( nil )
end

end – “return function()”

end
– Add defaultField
defaultField = native.newTextField( 10, 30, 180, 30,
fieldHandler( function() return defaultField end ) ) – passes the text field object

–Function to add to score and update scoreText
local function addToScore()
score = score + 1
scoreText.text = score
end
bg:addEventListener(“tap”, addToScore)

–Load highscore value from file. (It will initally be a string.)
highscore = loadFile (“highscore.txt”)

– Add score2
score2 = loadFile (“score2.txt”)

– Add defaultField
defaultField = loadFile (“field.txt”)

–If the file is empty (this means it is the first time you’ve run the app) save it as 0
local function checkForFile ()
if highscore == “empty” then
highscore = 0

end

–Add score2
if score2 == “empty” then
score2 = 4

end

–Add defaultField
if defaultField == “empty” then
defaultField = “text is empty”

saveFile(“highscore.txt”, highscore)

– Aad score2
saveFile(“score2.txt”, score2)

– Add defaultField
saveFile(“field.txt”, defaultField)

end
end
checkForFile()

–Print the current highscore
print (“Highscore is”, highscore)

– Add score2
print (“Score2 is”, score2)

– Add defaultField
print (“defaultField is”, defaultField)



–When the app is quit (or simulator refreshed) save the new highscore
–(If score > highscore the data will not be changed)

local function onSystemEvent ()
if score > tonumber(highscore) then --We use tonumber as highscore is a string when loaded
saveFile(“highscore.txt”, score)

– Add score2
saveFile(“score2.txt”, score2)

– Add defaultField
saveFile(“field.txt”, defaultField)
end
end
Runtime:addEventListener( “system”, onSystemEvent )[/code]

[import]uid: 150924 topic_id: 27333 reply_id: 111317[/import]

Hey, the first thing that stands out to me is that you refer to defaultField a few times when I think you actually want to refer to defaultField.text - defaultField is the field itself whereas defaultField.text is whatever text has been entered into the field.

Does that make sense? :slight_smile: [import]uid: 52491 topic_id: 27333 reply_id: 111465[/import]

peach , yes I understand it (for 70%) :slight_smile:

I made some changes. The keyboard keys will show in the Terminal. And the textfield on the screen displayed the text from the .txt file (loaded). But if I type something in the textfield, it will not save :frowning:

Updated code:

[code]–Hide the status bar
display.setStatusBar(display.HiddenStatusBar)

–Saving/Loading Stuff
local ego = require “ego”
local saveFile = ego.saveFile
local loadFile = ego.loadFile

–Create the background and make it blue
local bg = display.newRect( 0, 0, 320, 480 )
bg:setFillColor( 150, 180, 200 )

–Start the score at 1
local score = 1

– Add score2
local score2 = 2

–Create score text and make it dark gray
local scoreText = display.newText(score, 200, 20, native.systemFont, 24)
scoreText:setTextColor( 80, 80, 80 )

– Add score2
–Create score text and make it dark gray
local score2Text = display.newText(score, 200, 20, native.systemFont, 24)
scoreText:setTextColor( 80, 80, 80 )
– Add defaultField
local defaultField = “”-- forward reference (needed for Lua closure)

– TextField Listener
local function fieldHandler( getObj )

– Use Lua closure in order to access the TextField object

return function( event )

print( "TextField Object is: " … tostring( getObj() ) )

if ( “began” == event.phase ) then
– This is the “keyboard has appeared” event

elseif ( “ended” == event.phase ) then
– This event is called when the user stops editing a field:
– for example, when they touch a different field or keyboard focus goes away

print( "Text entered = " … tostring( getObj().text ) ) – display the text entered

elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key
– (if available) on the onscreen keyboard

– Hide keyboard
native.setKeyboardFocus( nil )
end

end – “return function()”

end
– Add defaultField
defaultField = native.newTextField( 10, 30, 180, 30,
fieldHandler( function() return defaultField.text end ) ) – passes the text field object

–Function to add to score and update scoreText
local function addToScore()
score = score + 1
scoreText.text = score
end
bg:addEventListener(“tap”, addToScore)

–Load highscore value from file. (It will initally be a string.)
highscore = loadFile (“highscore.txt”)

– Add score2
score2 = loadFile (“score2.txt”)

– Add defaultField
defaultField.text = loadFile (“field.txt”)

–If the file is empty (this means it is the first time you’ve run the app) save it as 0
local function checkForFile ()
if highscore == “empty” then
highscore = 0

end

–Add score2
if score2 == “empty” then
score2 = 4

end

–Add defaultField
if defaultField == “empty” then
defaultField = “text is empty”

saveFile(“highscore.txt”, highscore)

– Aad score2
saveFile(“score2.txt”, score2)

– Add defaultField
saveFile(“field.txt”, defaultField.text)

end
end

checkForFile()

–Print the current highscore
print (“Highscore is”, highscore)

– Add score2
print (“Score2 is”, score2)

– Add defaultField
print (“defaultField is”, defaultField.text)



–When the app is quit (or simulator refreshed) save the new highscore
–(If score > highscore the data will not be changed)

local function onSystemEvent ()
if score > tonumber(highscore) then --We use tonumber as highscore is a string when loaded
saveFile(“highscore.txt”, score)

– Add score2
saveFile(“score2.txt”, score2)

– Add defaultField
defaultField.text = saveFile(“field.txt”)
end
end
Runtime:addEventListener( “system”, onSystemEvent )[/code] [import]uid: 150924 topic_id: 27333 reply_id: 111493[/import]

Already got it working…sorry

Add this code to function of the fieldHandler

saveFile("field.txt", defaultField.text)

THANKS! [import]uid: 150924 topic_id: 27333 reply_id: 111494[/import]

Don’t be sorry - well done! It’s always awesome to figure something out on your own and even more so when that thing is very new to you.

Peach :slight_smile: [import]uid: 52491 topic_id: 27333 reply_id: 111500[/import]

Yes that’s right :slight_smile: That’s super cool :smiley: Try and error :slight_smile:

Last question about the textfields. I use this code, to load the text in screen A. And it’s works.

local myText = display.newRetinaText(loadFile ("field.txt"), 200, 200, native.systemFont, 12) myText:setTextColor(0)

But, this code does not for work? Because I want to show it only on Screen A and not on Screen B. If I use the localGroup code, the text is not displayed…

local myText = display.newRetinaText(loadFile ("field.txt"), 200, 200, native.systemFont, 12) myText:setTextColor(0) localGroup:insert(myText)

[import]uid: 150924 topic_id: 27333 reply_id: 111799[/import]

Is your background in the localGroup? Are you trying to display the text before or after you load the background image? Make sure is AFTER.

Let me know :slight_smile: [import]uid: 52491 topic_id: 27333 reply_id: 111923[/import]

@peach, sometimes it’s so simple, that you don’t think about it… hehe:) Thank you! [import]uid: 150924 topic_id: 27333 reply_id: 111967[/import]

I know, trust me - I’ve done that same thing plenty of times :slight_smile:

Good luck with the rest of your project! [import]uid: 52491 topic_id: 27333 reply_id: 112131[/import]

yes :slight_smile: Thank you

Maybe a simple thing, but I just upgrade to Indie Developer :smiley: And download the latest build. I open my project in the new version of Corona…and get a error in the Terminal:

WARNING: display.newRetinaText() has been deprecated. display.newText() is now retina-aware.

so I change my code:

local sampleText = display.newText(loadFile ("sample.txt"), 420, 430, native.systemFont, 16) sampleText:setTextColor(0) localGroup:insert(sampleText)

But the text is not displayed in the screen… [import]uid: 150924 topic_id: 27333 reply_id: 112148[/import]

Grats on upgrading to Indie!

I notice you changed the coords to 420, 430 from 200, 200 - what sized screen is this for? If it’s iPhone that would be off the screen. (In your original code this was for iPhone I notice as you create a rectangle for the background that is 320x480.)

Let me know if this is the case - if so is easy to fix by changing coords :slight_smile: [import]uid: 52491 topic_id: 27333 reply_id: 112257[/import]

Yes it was the case, thanks for all the support! :slight_smile: [import]uid: 150924 topic_id: 27333 reply_id: 112515[/import]

Not a problem :slight_smile: [import]uid: 52491 topic_id: 27333 reply_id: 112618[/import]