widget.tableView and sqlite

Anyone have a working example of how to load a sqlite database that has categories and the corresponding functions for non category entries so that it loads into the new tableView widget…

Matt [import]uid: 18783 topic_id: 23297 reply_id: 323297[/import]

Hey, this should hopefully give you a generally idea… But this example doesn’t have categories in… it just gets a name from a database and displays it in a general tableview.

Maybe if i get some time i’ll do categories too :smiley:

[code]

–Set some basic vars…
local siteName = {};
local int = 1

–Go through my database selecting all the site names…
local dbPath = system.pathForFile(“dipInfo.db3”, system.DocumentsDirectory)
local db = sqlite3.open( dbPath)
for row in db:nrows(“SELECT * FROM siteNames”) do
siteName[int] = row.name
int = int+1
end
db:close()

–Now create the widget.newTableView…
local listOptions = {
width = 320,
height = 420,
top = 46,
bgColor = {255,255,255,1},
maskFile = “images/maskfile.png”
}
list = widget.newTableView( listOptions )

–What happens when you touch a row…
local function onRowTouch( event )
local row = event.target; local rowGroup = event.view

if event.phase == “press” then
rowGroup.alpha = 0.5

elseif event.phase == “swipeLeft” or event.phase == “swipeRight” then
rowGroup.alpha = 1;
–here you could display a delete row button…

elseif event.phase == “release” or event.phase == “tap” then
–Now you can do whatever you want when you tap or click this row…
–maybe… event.target.name to get the name of the row you clicked etc.
end
return true
end

–The creation of each row…
local function onRowRender( event )
local row = event.target; row.id = event.index
local rowGroup = event.view

local circle = display.newCircle( 0, 0, 3 )
circle.y = row.height * 0.5; circle.x = 20; circle:setFillColor(1,87,141)

–Disply the sitename we got from our database…
local text = display.newRetinaText( siteName[event.index], 12, 0, “Helvetica”, 16 )
text:setReferencePoint( display.CenterLeftReferencePoint )
text.y = row.height * 0.5; text.x = 36; text:setTextColor(1,87,141,255)

row.name = siteName[event.index]
rowGroup:insert( circle ); rowGroup:insert( text )
end

–Go through the array we created making rows.
for i=1,#siteName do
list:insertRow{
onEvent=onRowTouch,
onRender=onRowRender,
height=58,
rowColor={255,255,255,25},
lineColor={200,200,200}
}
end
mainGroup:insert( list )
[/code] [import]uid: 69826 topic_id: 23297 reply_id: 93304[/import]

So how would it handle the ontouch event say… each item in the database has a function that fires when it is touched.

BTW: Thanks for the snippet it already is making this ideas easier.

[import]uid: 18783 topic_id: 23297 reply_id: 93307[/import]

No problem.

So when each row is touched you want a different function to fire?

You could do that multiple ways really… but the easiest and less time consuming way would be to get the id or name of the row touched, check what it was, then fire off different functions depending.
So…

local function onRowTouch( event )  
 --We will use row below, to save us typing event.target everytime.  
 local row = event.target; local rowGroup = event.view  
   
 if event.phase == "press" then  
 rowGroup.alpha = 0.5  
  
 elseif event.phase == "release" or event.phase == "tap" then  
 --In the example above i stored a reference to the items name in each row.  
 --So for simplicities sake you could just check against that. eg.  
  
 if row.name == "exampleRow" then  
 exampleFunction()  
 elseif row.name == "corona" then  
 coronaFunction()  
 else  
 otherDefaultFunction()  
 end  
 end   
return true  
end  

Is that what your on about? or have i missed the idea completely lol?
[import]uid: 69826 topic_id: 23297 reply_id: 93311[/import]

yes… but lets put it this way… using the old coronaui you could just put each item for the row with data and associate an onTouch event with each row…
like

Cool place to go

showCoolplace

then when the file is loaded or the items added the touching of the row called the function showCoolplace()… each row had a different function because it showed different data for example. [import]uid: 18783 topic_id: 23297 reply_id: 93533[/import]