TableView Options on First App

Hello,

My name is Preston. I have just recently learned corona and am working on a project to build my very first app that is publishable. I am having some problems and wondering how I can do somethings that I didn’t learn in the book. 

My App

I am trying to make an app that is for Ranchers that have cattle. I want to be able to add a birth and have a textField for the “Cows ID”, “Age”, “Calf ID”, “Sex”, “Date”, “Weight”, “Sire”, and then any “Notes”. I want a “save” and “cancel” button. The save button will “save” to a database and the “cancel” button will cancel and return to the menu. That is one thing I’m having a problem with. I can’t implement a close button and a save button. I guess I don’t know the best way to implement both. 

After the save button is tapped it will save the data into a table and will be displayed in another button at the menu called “Herd Data”. I don’t know how to set up a table correctly. I wanted the table to have the Cows ID" and the "Calf ID" displayed and then when tapped it displays the rest of the information added, but I didn’t know how to do that and couldn’t find an example on the blog here at Corona. I would just like a little guidance, maybe some examples or just some good conversation and input of what I could do to make it better and easier. 

Thank you so much!

I will copy and paste what code I have now. What I have right now throws an error for my “path” created for the file. I don’t understand why, but after that it crashes and I have to restart it. Thank you again… just need a little help I guess.

Thanks again


– main.lua


– Your code here

local storyboard = require(“storyboard”)

storyboard.gotoScene(“menu”)


– menu.lua


local widget = require (“widget”)

local storyboard = require(“storyboard”)

local scene = storyboard.newScene()

local localGroup = display.newGroup()

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

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 DAT 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

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

It would be very helpful if you did the following;

1.  Edit your post and put and tags around each block of code (each .lua file in this case). Leave the spaces after the [ out.

  1. Next read this tutorial on debugging:

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

  1. Since you say it’s not working any one needing such help, we need to know more specifically about what’s happening.  Perhaps you have an error that shows up in your console logs (ergo the need for step #2).  Right now this is something that any community member  will struggle answering. 

Between the lack of code formatting (#1), the lack of any idea about what’s not working correctly (#2) and the large amount of code you posted, people are going to look at this thread and say “No thanks” and move along.  If you provide the community with something to help you, they will respond.  It’s also okay to bump a post after a day or so if you didn’t get an answer.  There are many posts and it’s okay to bump it to bring it back in front of the community.

Rob

Preston,

If you are just starting with Corona, you may want to consider using ‘Composer’ instead of ‘Storyboard’.  Composer is the newer(improved) version of Storyboard. I do suggest, re-doing you code using Composer, even though you may need to re-type a lot of your code, that is the way to go. Search corona site for composer samples.

Here is a link about Composer :  http://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

You question regarding tables, I guess you are speaking of ‘sql tables’.  I am not real experienced with sql, so I really can’t shed much light on that.  

This link ‘may’ help regarding sql tables  :  http://stackoverflow.com/questions/23370896/inserting-data-into-sqllite-database-with-corona-sdk-simulator

Goodluck

It would be very helpful if you did the following;

1.  Edit your post and put and tags around each block of code (each .lua file in this case). Leave the spaces after the [ out.

  1. Next read this tutorial on debugging:

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

  1. Since you say it’s not working any one needing such help, we need to know more specifically about what’s happening.  Perhaps you have an error that shows up in your console logs (ergo the need for step #2).  Right now this is something that any community member  will struggle answering. 

Between the lack of code formatting (#1), the lack of any idea about what’s not working correctly (#2) and the large amount of code you posted, people are going to look at this thread and say “No thanks” and move along.  If you provide the community with something to help you, they will respond.  It’s also okay to bump a post after a day or so if you didn’t get an answer.  There are many posts and it’s okay to bump it to bring it back in front of the community.

Rob

Preston,

If you are just starting with Corona, you may want to consider using ‘Composer’ instead of ‘Storyboard’.  Composer is the newer(improved) version of Storyboard. I do suggest, re-doing you code using Composer, even though you may need to re-type a lot of your code, that is the way to go. Search corona site for composer samples.

Here is a link about Composer :  http://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

You question regarding tables, I guess you are speaking of ‘sql tables’.  I am not real experienced with sql, so I really can’t shed much light on that.  

This link ‘may’ help regarding sql tables  :  http://stackoverflow.com/questions/23370896/inserting-data-into-sqllite-database-with-corona-sdk-simulator

Goodluck