TableView and SQLite using Widget 2.0

Hello everyone,

I have been trying for hours to get data from an SQL Database to insert into a tableView using the Widget 2.0 library and I can’t seem to figure out how to do it. All I’m getting right now is multiple versions of the most recently entered value (rather than each value in the SQL table). Can anyone provide a basic example for inserting data from a SQL Database into a tableview? I would greatly appreciate any help or advice.

Thank you

Can you post your code?

= row.name i=i+1 nameText = row.name print(nameText) end -- Forward reference for our tableView local tableView = nil -- Listen for tableView events local function tableViewListener( event ) local phase = event.phase print( "Event.phase is:", event.phase ) end -- Handle row rendering local function onRowRender( event ) local phase = event.phase local row = event.row local rowTitle = display.newText( row, ""..nameText, 0, 0, nil, 14 ) rowTitle.x = row.x - ( row.contentWidth \* 0.5 ) + ( rowTitle.contentWidth \* 0.5 ) + LEFT\_PADDING rowTitle.y = row.contentHeight \* 0.5 rowTitle:setTextColor( 245, 245, 245 ) end -- Handle row updates local function onRowUpdate( event ) local phase = event.phase local row = event.row --print( row.index, ": is now onscreen" ) end -- Handle touches on the row local function onRowTouch( event ) local phase = event.phase local row = event.target if "release" == phase then --Update the item selected text itemSelected.text = "You selected item " .. row.index --Transition out the list, transition in the item selected text and the back button transition.to( tableView, { x = - tableView.contentWidth, time = 400, transition = easing.outExpo } ) transition.to( itemSelected, { x = 156, time = 400, transition = easing.outExpo } ) transition.to( backButton, { x = 156, time = 400, transition = easing.outQuad } ) end end -- Create a tableView tableView = widget.newTableView { top = 76, width = 312, height = 692, maskFile = "mask-312x692.png", listener = tableViewListener, hideBackground = true, noLines = true, onRowRender = onRowRender, onRowUpdate = onRowUpdate, onRowTouch = onRowTouch, } -- Create 100 rows for i=1, #pulledCategories do local isCategory = false local rowHeight = 50 local rowColor = { default = { 51, 51, 51 }, over = { 30, 144, 255 }, } local lineColor = { 220, 220, 220 } -- Insert the row into the tableView tableView:insertRow { isCategory = isCategory, rowHeight = rowHeight, rowColor = rowColor, lineColor = lineColor, } end

I hope this helps. The correct number of rows appear in the tableView, but the most recent value appears multiple times. I’ve attached a picture of what it looks like.

Thank you

for row in db:nrows("SELECT \* FROM categories") do pulledCategories[i]= row.name i=i+1 nameText = row.name print(nameText) end

local function onRowRender( event ) local phase = event.phase local row = event.row local rowTitle = display.newText( row, ""..nameText, 0, 0, nil, 14 ) rowTitle.x = row.x - ( row.contentWidth \* 0.5 ) + ( rowTitle.contentWidth \* 0.5 ) + LEFT\_PADDING rowTitle.y = row.contentHeight \* 0.5 rowTitle:setTextColor( 245, 245, 245 ) end

Here’s your problem. You’re drawing your row using nameText instead of pulledCategories[n]. In your tableView rowInsert loop, try setting an id = i. which you can use in your onRowRender as a table index for pulledCategories.

That did it! Thank you. I do have one more question. After I add a new row to the table, it only displays in the tableView after I completely reload the project. Is there any way to force it to reload so that the new entry appears after it is added to the database?

Thanks again.

This thread might help you : http://forums.coronalabs.com/topic/36881-tableview-how-to-force-re-rendering/?p=191083

You can select the new rows from database and do insertRow on those new rows and it will be appended to the bottom of the table.

Or you can put your read from SQL and insertRow code into a seperate function, put tableView:deleteAllRows() at the top and just run the function whenever you need fresh contents.

@jonjonsson…I ended up doing something similar to what you suggested and it worked perfectly! Thank you so much to everyone who helped me out with this issue. 

Can you post your code?

= row.name i=i+1 nameText = row.name print(nameText) end -- Forward reference for our tableView local tableView = nil -- Listen for tableView events local function tableViewListener( event ) local phase = event.phase print( "Event.phase is:", event.phase ) end -- Handle row rendering local function onRowRender( event ) local phase = event.phase local row = event.row local rowTitle = display.newText( row, ""..nameText, 0, 0, nil, 14 ) rowTitle.x = row.x - ( row.contentWidth \* 0.5 ) + ( rowTitle.contentWidth \* 0.5 ) + LEFT\_PADDING rowTitle.y = row.contentHeight \* 0.5 rowTitle:setTextColor( 245, 245, 245 ) end -- Handle row updates local function onRowUpdate( event ) local phase = event.phase local row = event.row --print( row.index, ": is now onscreen" ) end -- Handle touches on the row local function onRowTouch( event ) local phase = event.phase local row = event.target if "release" == phase then --Update the item selected text itemSelected.text = "You selected item " .. row.index --Transition out the list, transition in the item selected text and the back button transition.to( tableView, { x = - tableView.contentWidth, time = 400, transition = easing.outExpo } ) transition.to( itemSelected, { x = 156, time = 400, transition = easing.outExpo } ) transition.to( backButton, { x = 156, time = 400, transition = easing.outQuad } ) end end -- Create a tableView tableView = widget.newTableView { top = 76, width = 312, height = 692, maskFile = "mask-312x692.png", listener = tableViewListener, hideBackground = true, noLines = true, onRowRender = onRowRender, onRowUpdate = onRowUpdate, onRowTouch = onRowTouch, } -- Create 100 rows for i=1, #pulledCategories do local isCategory = false local rowHeight = 50 local rowColor = { default = { 51, 51, 51 }, over = { 30, 144, 255 }, } local lineColor = { 220, 220, 220 } -- Insert the row into the tableView tableView:insertRow { isCategory = isCategory, rowHeight = rowHeight, rowColor = rowColor, lineColor = lineColor, } end

I hope this helps. The correct number of rows appear in the tableView, but the most recent value appears multiple times. I’ve attached a picture of what it looks like.

Thank you

for row in db:nrows("SELECT \* FROM categories") do pulledCategories[i]= row.name i=i+1 nameText = row.name print(nameText) end

local function onRowRender( event ) local phase = event.phase local row = event.row local rowTitle = display.newText( row, ""..nameText, 0, 0, nil, 14 ) rowTitle.x = row.x - ( row.contentWidth \* 0.5 ) + ( rowTitle.contentWidth \* 0.5 ) + LEFT\_PADDING rowTitle.y = row.contentHeight \* 0.5 rowTitle:setTextColor( 245, 245, 245 ) end

Here’s your problem. You’re drawing your row using nameText instead of pulledCategories[n]. In your tableView rowInsert loop, try setting an id = i. which you can use in your onRowRender as a table index for pulledCategories.

That did it! Thank you. I do have one more question. After I add a new row to the table, it only displays in the tableView after I completely reload the project. Is there any way to force it to reload so that the new entry appears after it is added to the database?

Thanks again.

This thread might help you : http://forums.coronalabs.com/topic/36881-tableview-how-to-force-re-rendering/?p=191083

You can select the new rows from database and do insertRow on those new rows and it will be appended to the bottom of the table.

Or you can put your read from SQL and insertRow code into a seperate function, put tableView:deleteAllRows() at the top and just run the function whenever you need fresh contents.

@jonjonsson…I ended up doing something similar to what you suggested and it worked perfectly! Thank you so much to everyone who helped me out with this issue.