I’ve been trying to get the TableView working with an SQLite database. I’ve managed to get the data into the TableView but am finding some weird issues:
- Whenever I scroll the TableView the rows are re-rended and show data from the very last row returned from the select statement.
- My Categories don’t ‘stick’ to the top when scolling.
Could someone please help? My code is below.
Regards,
Trent
local widget = require( “widget” )
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local testSQLText = display.newText( “”, 0, 0, native.systemFont, 12)
local serverPath
local myResponse
local evacpath = system.pathForFile( “evaclist.sqlite”, system.TemporaryDirectory )
local evacTableView = nil
local givenname
local familyname
local mobileno
local host
local status
local persontype
local cd
– Our scene
function scene:createScene( event )
local group = self.view
----!!! Listeners !!!----
– Listen for evacTableView events
local function evacTableViewListener( event )
local phase = event.phase
print( “Event.phase is:”, event.phase )
end – function
– Handle row rendering
local function onRowRender( event )
local phase = event.phase
local row = event.row
rowTitle = display.newText( row, givenname…" "…familyname, 0, 0, nil, 16 )
rowTitle.x = row.x - ( row.contentWidth * 0.5 ) + ( rowTitle.contentWidth * 0.5 ) + 20
rowTitle.y = row.contentHeight * 0.25
if event.row.isCategory == true then
rowTitle.x = display.contentCenterX
rowTitle.y = row.contentHeight * 0.5
end --if
rowTitle:setTextColor( 0, 0, 0 )
print("rowtitle.x = " …rowTitle.x)
if cd ~= nil then
rowCD = display.newText( row, cd, 0, 0, nil, 12 )
rowCD.x = row.x - ( row.contentWidth * 0.5 ) + ( rowCD.contentWidth * 0.5 ) + 20
rowCD.y = rowTitle.y + 15
rowCD:setTextColor( 0, 0, 0 )
print("rowCD.x = " …rowCD.x)
end --if
if mobileno ~= nil then
rowMobile = display.newText( row, mobileno, 0, 0, nil, 12 )
rowMobile.x = row.x - ( row.contentWidth * 0.5 ) + ( rowMobile.contentWidth * 0.5 ) + 20
rowMobile.y = rowTitle.y + 30
rowMobile:setTextColor( 0, 0, 0 )
print("rowmobile.x = " …rowMobile.x)
end --if
end – function
– Handle touches on the row
local function onRowTouch( event )
local phase = event.phase
local row = event.target
if “press” == event.phase then
print( “Touched row:”, event.target.index )
end – if
end – function
– Handles row update
local function onRowUpdate( event )
local phase = event.phase
local row = event.row
end
local function networkEvacListener( event )
if ( event.isError ) then
testSQLText.text = “Network Error 2!”
elseif event.phase == “began” then
testSQLText.text = “Downloading…”
elseif event.phase == “ended” then
print(“Received file: evaclist.sqlite”)
testSQLText.text = “Download Complete!”
loadSQLList()
end --if
end – function
local function networkListener( event )
if ( event.isError ) then
testSQLText.text = “Network Error 1!”
else
myResponse = event.response
if tostring(myResponse) == “1” then
testSQLText.text = “Device not recognised.”
elseif tostring(myResponse) == “2” then
testSQLText.text = “Device not registered.”
else
testSQLText.text = “No errors received!”
print(myResponse)
local params = {}
params.progress = true
network.download( “http://”…serverPath…"/SendEvacuationList?name=" …myResponse, “GET”, networkEvacListener, params, “evaclist.sqlite”, system.TemporaryDirectory)
end – if
end – if
end – function
----!!! Functions !!!----
function backToMenu()
transition.to( group, { time = 1000, x = 0 - display.contentWidth, onComplete = function() storyboard.gotoScene( “menu” ); end })
end – function
function testSQL()
deviceID = system.getInfo( “deviceID” )
db = sqlite3.open( path )
for a in db:nrows(“SELECT * FROM settings”) do
serverPath = a.ServerPath
end – for
db:close()
if serverPath == nil then
testSQLText.text = “No Server Path found. Please enter the Server Path in Settings.”
else
testSQLText.text = “Connecting… please wait.”
end – if
testSQLText.x = display.contentCenterX
testSQLText.y = display.contentHeight - (testSQLText.contentHeight / 2)
testSQLText:setTextColor(0,0,0)
--delete these rows for production
deviceID = 1
serverPath = “localhost:85”
– do it or else
if serverPath ~= nil then
network.request( “http://”…serverPath…"/EvacuationListMobile?did=" …deviceID, “GET”, networkListener)
end – if
end
function loadSQLList()
evacdb = sqlite3.open( evacpath )
local function onSystemEvent( event )
if event.type == “applicationExit” then
evacdb:close()
end – if
end – function
for b in evacdb:nrows(“SELECT * FROM Visitor ORDER BY PersonType, Status, GivenName, FamilyName”) do
sysID = b.SysID
givenname = b.GivenName
familyname = b.FamilyName
cd = b.CD
mobileno = b.MobileNo
host = b.Host
status = b.Status
persontype = b.PersonType
local isCategory = false
local rowHeight = 75
local rowColor =
{
default = { 255, 255, 255 },
}
local lineColor = { 220, 220, 220 }
if (givenname == “Visitors” and familyname == “On Site”) or (givenname == “Personnel” and familyname == “On Site”) then
isCategory = true
rowHeight = 50
rowColor =
{
default = {212, 212, 212, 200},
}
end --if
evacTableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
}
end – for
evacdb:close()
end – function
----!!! Menu and Back Button !!!----
menuHeaderGroup = display.newGroup ( )
– The gradient used by the title bar
local titleGradient = graphics.newGradient(
{ 189, 203, 220, 255 },
{ 89, 116, 152, 255 }, “down” )
– Create a title bar
local titleBar = display.newRect( 0, 0, display.contentWidth, 32 )
titleBar.y = titleBar.contentHeight / 2
titleBar:setFillColor( titleGradient )
menuHeaderGroup:insert(titleBar)
– Create the title bar text
local titleBarText = display.newEmbossedText( “visitor id”, 0, 0, native.systemFontBold, 24 )
titleBarText.x = titleBar.x
titleBarText.y = titleBar.y
menuHeaderGroup:insert(titleBarText)
– add back button
local backButton = widget.newButton
{
left = 0,
top = 0,
width = 50,
height = 30,
default = { 255, 255, 255, 90 },
over = { 120, 53, 128, 255 },
fontSize = 12,
label = “Back”,
onRelease = backToMenu,
}
backButton.x = display.contentWidth - (backButton.contentWidth / 2)
– Create controls
evacTableView = widget.newTableView
{
top = titleBar.contentHeight,
width = 320,
height = 400,
--backgroundColor = { 255, 255, 255 },
listener = evacTableViewListener,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
onRowUpdate = onRowUpdate,
}
group:insert(evacTableView)
group:insert(menuHeaderGroup)
group:insert(backButton)
group:insert(testSQLText)
group.x = display.contentWidth * 1.5
transition.to( group, {time = 1000, x = 0, onComplete = testSQL})
end – function
scene:addEventListener( “createScene” )
function scene:destroyScene( event )
print( “Called when scene is unloaded.” )
evacdb:close()
end
scene:addEventListener( “destroyScene” )
return scene