Hello,
My name is Preston. I am having some problems building my very first publishable app. I just recently learned Corona and am going off on my own to build this app. I am trying to build app for ranchers that raise cattle during calving (birth) season. What I want to do is have a menu that has buttons that will take you to different locations and other luas. I want an “Add Birth Button” that will take the user to a place where they can enter the “_Cow ID”, “Age”, “Calf ID”, “Sex”, “Date”, “Weight”, _and the “Sire”. Then I will place a “Save Button” and a “Cancel Button” at the bottom of the page.
This is my first problem. I can’t add a "Cancel Button" and a "Save Button" at the same time in a storyboard. I guess I don’t know the correct way to do so the "Cancel Button" will take the user back to the menu.lua and cancel out of the entry.
The "Save Button" will save the information entered into a database and take the user back to the menu page. A "Herd Data" Button will be put in place which is where I will display a tableView, in another lua, of the information the user entered.
This is where I run into my main problem. I don’t know how I would display this information in a manner to be readable and easy to use for the user. I want it to be a table with just the “_Cow ID” _and the "Calf ID" and then the user "Taps" the "Cow ID" and the “_Calf ID” _they want to look at and it pops up another window and it displays the rest of the information. I would also like to add an “_Edit Button” _to allow the user to delete or edit the information. I don’t know the best manner to create a table like this. I have looked over countless information and tutorials on Youtube, Google, and the Corona Website and Blogs. I haven’t found anything that would help me with how to create a good table for this type of action. I guess I wanted to see if anyone had some examples or sample code of a good table to use or just spark some good brainstorming conversation on other directions I could take this on or what is the best way to display this information. Maybe a spreadsheet? But I don’t know how that would work either…
Thank you all!
I will paste my code that I have so far for discussion purposes. Thank you.
PS - I get an error with my code when I run it that states: File:
addBirth.lua
Line: 84
Bad argument #1 to ‘open’ (string expected, got nil)
I then try to relaunch the app and it crashes… I can’t debug my problem… Guess I have a lot of issues…
Thanks for the help.
–
– main.lua
– This will start the storyboard and call the menu scene
local storyboard = require(“storyboard”)
storyboard.gotoScene(“menu”)
local widget = require (“widget”)
local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
local localGroup = display.newGroup()
–
– menu.lua
– This will be the display page for all the buttons that will take the user to other sections of luas to input and display information
function scene:createScene(event)
local background = display.newImage(“barn.jpg”, display.contentCenterX, display.contentCenterY ) – WORK ON!! **
local display_stage = display.getCurrentStage()
display_stage:insert(background)
display_stage:insert(storyboard.stage)
background:toBack()
local herdData_function = function (event)
storyboard.gotoScene(“herdData”, “fade”, 400)
end
local herdData_button = widget.newButton
{
defaultFile = “Button.png”,
label = “Herd Data”,
size = 30,
fontSize = 60,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = herdData_function,
id = “herdData”,
}
local addBirth_function = function(event)
storyboard.gotoScene(“addBirth”, “fade”, 400)
end
local addBirth_button = widget.newButton
{
defaultFile = “Button.png”,
label = “Add Birth”,
size = 30,
fontSize = 60,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = addBirth_function,
id = “addBirth”,
}
local doctorVisit_function = function (event)
storyboard.gotoScene(“doctorVisit”, “fade”, 400)
end
local doctorVisit_button = widget.newButton
{
defaultFile = “Button2.png”,
label = “Doctor Visit”,
labelsize = 24,
fontSize = 40,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = doctorVisit_function,
id = “doctorVisit”,
}
local doctorRecords_function = function (event)
storyboard.gotoscene(“doctorRecords”, “fade”, 400)
end
local doctorRecords_button = widget.newButton
{
defaultFile = “Button2.png”,
label = “Doctor Records”,
size = 24,
fontSize = 40,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = doctorRecords_function,
id = “doctorRecords”,
}
addBirth_button.x = display.contentWidth/2
addBirth_button.y = display.contentHeight/2 -350
doctorVisit_button.x = display.contentWidth/2
doctorVisit_button.y = display.contentHeight/2 +10
doctorRecords_button.x = display.contentWidth/2
doctorRecords_button.y = display.contentHeight/2-150
herdData_button.x = display.contentWidth/2
herdData_button.y = display.contentWidth/2 + 500
localGroup:insert(addBirth_button)
addBirth_button:toFront()
localGroup:insert(doctorVisit_button)
localGroup:insert(doctorRecords_button)
localGroup:insert(herdData_button)
end
function scene:enterScene(event)
localGroup.alpha = 1
storyboard.purgeAll()
end
function scene:exitScene(event)
localGroup.alpha = 0
end
– “createScene” is called whenever the scene is FIRST called
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
return scene
– addBirth.lua
– Add Data to the table created
local widget = require(“widget”)
local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
local localGroup = display.newGroup()
– require sqlite3
require “sqlite3”
– called if the cene has not been previously seen
function scene:createScene(event)
– Add text boxes to enter the data
local title = display.newText(localGroup, “*Congratulation on the new Birth!!*”, display.contentWidth/2, 100, native.systemFont, 30)
title:setFillColor(1,0,2)
local cowIDLabel = display.newText(localGroup, “Cow ID:”, 155, 200, native.systemFont, 30)
cowIDLabel:setFillColor(255,223,0)
local ageLabel = display.newText(localGroup, “Age:”, 425, 200, native.systemFont, 30)
ageLabel:setFillColor(255,223,0)
local calfIDLabel = display.newText(localGroup, “Calf ID:”, 155, 300, native.systemFont, 30)
calfIDLabel:setFillColor(255,223,0)
local sexLabel = display.newText(localGroup, “Sex:”, 425, 300, native.systemFont, 30)
sexLabel:setFillColor(255,223,0)
local dateLabel = display.newText(localGroup, “Date:”, 140, 400, native.systemFont, 30)
dateLabel:setFillColor(255,223,0)
local weightLabel = display.newText(localGroup, “Weight:”, 155, 500, native.systemFont, 30)
weightLabel:setFillColor(255,223,0)
local sireLabel = display.newText(localGroup, “Sire:”, 425, 500, native.systemFont, 30)
sireLabel:setFillColor(255,223,0)
– NOTES NEED TO BE SCROLLABLE!!
local notesLabel = display.newText(localGroup, “Notes”, display.contentWidth/2, 600, native.systemFont, 30)
notesLabel:setFillColor(255,223,0)
local line = display.newLine(340, 615, 425, 615)
line:setStrokeColor(255,233,0)
line.strokeWidth = 3
local cowID = native.newTextField(290, 200, 150, 45)
cowID.inputType = “default”
localGroup:insert(cowID)
local age = native.newTextField(550, 200, 150, 45)
age.inputType = “default”
localGroup:insert(age)
local calfID = native.newTextField(290, 300, 150, 45)
calfID.inputType = “default”
localGroup:insert(calfID)
local sex = native.newTextField(550, 300, 150, 45)
sex.inputType = “default”
localGroup:insert(sex)
– MAKE A PICKER WHEEL FOR THE DATe AND SET IT TO DEFAULT TO THE CURRENT DAY THAT DAY
local date = native.newTextField(420, 400, 410, 50)
date.inputType = “default”
localGroup:insert(date)
local weight = native.newTextField(290, 500, 150, 45)
weight.inputType = “default”
localGroup:insert(weight)
local sire = native.newTextField(550, 500, 150, 45)
sire.inputType = “default”
localGroup:insert(sire)
– NOTES NEED TO BE SCROLLABLE WHEN ENTERED
local notes = native.newTextField(display.contentWidth/2, 710, 400, 100)
notes.inputType = “default”
localGroup:insert(notes)
local addBirth_function = function(event)
– open sqlite database if it does not exist
local path = system.pathForFile(“herd.sqlite”, systemDocumentsDirectory)
db = sqlite3.open(path)
– check that the path was made
print(path)
– setup the table if it does not exist
local tablesetup = “CREATE TABLE IF NOT EXISTS birthclass (id INTEGERS PRIMARY KEY AUTOINCREMENT, CowId, Age, CalfId, Sex, Date, Weight, Sire, Notes);”
db = sqlite3.open(path)
– check that it created the table
print(path)
local tablefill = “INSERT INTO birthclass VALUES (NULL, '” …cowID.text… “’,’”…age.text…"’,’"…calfID.text…"’,’"…sex.text…"’,’"…date.text…"’,’"…weight.text…"’,’"…sire.text…"’,’"…notes…"’);"
– check that it filled the table
print(tablefill)
db:exec(tablefill)
– close database
db:close()
– check that it closed
print(“db closed”)
– clear textFields and return to menu screen
cowID:removeSelf()
age:removeSelf()
calfID:removeSelf()
sex:removeSelf()
date:removeSelf()
weight:removeSelf()
sire:removeSelf()
notes:removeSelf()
end – submitBirth_function
local saveBirth_button = widget.newButton
{
defaultFile = “Save.png”,
overFile = “Save.png”,
label = “Save”,
fontSize = 65,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = addBirth_function,
id = “addBirth”
}
saveBirth_button.x = display.contentWidth/2
saveBirth_button.y = display.contentHeight/2 +350
– add all display items to the group
localGroup:insert(saveBirth_button)
– handle the applicationExit scene to close db
local function onSystemEvent(event)
if(event.type == “applicationExit”) then
db:close()
end
end
– system listener for applicationExit to handle closing db
Runtime:addEventListener (“system”, onSystemEvent)
end
function scene:enterScene(event)
storyboard.purgeScene(“menu”)
localGroup.alpha=1
end
function scene:exitScene(event)
localGroup.alpha = 0
end
– “createScene” is called whenever the scene is FIRST called
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
return scene
– herdData.lua
– Hold the herds Data entered by the user
– This was the way that I tried to do my table and it didn’t work the way I wanted it to.
local widget = require (“widget”)
local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
local localGroup = display.newGroup()
– include sqlite
require “sqlite3”
– called if the scene hasnt been seen
function scene:createScene(event)
– open database
local pathe = system.pathForFile(“students.sqlite”, system.DocumentsDirectory)
db = sqlite3.open(path)
print(path)
– print all the table contents
local sql = “SELECT * FROM herd”
for row in db:nrows(sql) do
local text = row.CowID…", “…row.Age…”, “…row.CalfID…”, “…row.Sex…”, “…row.Date…”, “…row.Weight…”, “…row.Sire…”, "…row.Notes
local t = display.newText(text, 200, 30 *row.id, native.systemFont, 24)
t:setTextColor(255,255,255)
localGroup:insert(t)
end
– setup function for button to load student data
local herdData_function = function(event)
– return to menu screen
storyboard.gotoScene(“menu”, “slideRight”, 400)
end
local herdData_button = widget.newButton
{
defaultFile = “Button2.png”,
overFile = “Button2.png”,
label “Return to Menu”,
fontSize = 50,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = herdData_function,
id = “herdData”,
}
herdData_button.x = display.contentWidth/2 + 200
herdData_button.x = display.contentHeight/2 - 400
– add to local group
localGroup:insert(addHerd_button)
– handle the applicationexit event to close db
local function onSystemEvent(event)
if(event.type == “applicationExit”) then
db:close()
end
end
– system listener for applicationExit to handle closing database
Runtime:addEventListener (“system”, onSystemEvent)
end
function scene:enterScene(event)
storyboard.purgeScene(“menu”)
localGroup.alpha = 1
end
function scene:exitScene(event)
localGroup.alpha=0
end
– “createScene” is called whenever the scene is FIRST called
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
return scene