DB Data Not Displaying with Widgets 2.0 TableView

I think that I’ve got my code almost figured out, but my simulator screen remains blank. Although, it responds as if the data is there (scrolls a long way).

I did a print test near the bottom of my code and the database does appear on the output window.

Here are the last few lines of my code:

print ( listRecs[x].rowid, listRecs[x].date, listRecs[x].title ) end end -- showRecords db = setUpDatabase("mydatabase.sqlite") setupInterface() loadData() showRecords()

And, like I said, that print statement spits out the correct data. I have wondered if it’s a color problem, but I don’t know enough, I guess. I think this is the relevant code for the display problem. If I should look somewhere else, please let me know.

local function showRecords() local function onRowRender( event ) local phase = event.phase local row = event.row local rowGroup = event.view local idx = event.index --add event.index to work with widgets 2.0 local color = {0,0,0} --widgets 2.0 db:insertRow{ -- changed list to db rowHeight=rowHeight, isCategory=isCategory, --rowColor=rowColor, lineColor=lineColor, } -- title to display row.textObj = display.newRetinaText( listRecs[idx].title, 0, 0, "Helvetica", 16 ) row.textObj:setTextColor( color ) row.textObj:setReferencePoint( display.CenterLeftReferencePoint ) row.textObj.x = 20 row.textObj.y = rowGroup.contentHeight \* 0.35 -- add date to display row.textObj2 = display.newRetinaText( listRecs[idx].date, 0, 0, "Helvetica", 12 ) row.textObj2:setTextColor( color ) row.textObj2:setReferencePoint( display.CenterLeftReferencePoint ) row.textObj2.x = 20 row.textObj2.y = rowGroup.contentHeight \* 0.65 local function delRow( event ) print("Delete hit: " .. tostring(event.target.id)) local dbid = listRecs[event.target.id].id list:deleteRow(event.target.id) --remove from list widget table.remove(listRecs, event.target.id) --remove from table display.remove( detailGrp ) -- delete from database function deletData(id) local sql = "delete from News where id = " .. tostring(id) db:exec(sql) end deleteData(dbid) -- end row.delButton = widget.newButton{ -- check with Widgets 2.0 docs id = row.index, top = rowGroup.contentHeight \* 0.2, left = rowGroup.contentWidth - 90, default = "deletebtn.png", width = 64, height = 33, onRelease = delRow } row.delButton.alpha = 0 if listRecs[idx].showDel == true then row.delButton.alpha = 1 end rowGroup:insert(row.delButton) rowGroup:insert(row.textObj) rowGroup:insert(row.textObj2) -- display doesn't work. Refer to Widgets 2.0 documentation end --onRowRender local function rowListener( event ) local background = event.background local row = event.row local phase = event.phase if phase == "press" then print( "Pressed row: " .. row.index ) background:setFillColor( 196, 255, 156, 255 ) elseif phase == "release" or phase == "tap" then print( "Tapped and/or Released row: " .. row.index ) background:setFillColor( 196, 255, 156, 255 ) row.reRender = true -- go to new scene or add details to part of screen showDetails(row.index) elseif phase == "swipeLeft" then print( "Swiped Left row: " .. row.index ) listRecs[row.index].showDel = true row.reRender = true elseif phase == "swipeRight" then print( "Swiped Right row: " .. row.index ) listRecs[row.index].showDel = false display.remove( row.delButton ) end end -- rowListener for x = 1, #listRecs do list:insertRow { onRender = onRowRender, listener = rowListener } print ( listRecs[x].rowid, listRecs[x].date, listRecs[x].title ) end end -- showRecords

It looks like you’re using rowGroup = event.view and I think that’s one of the things that was deleted from widgets 2.0. Try just inserting the objects into row instead of rowGroup, and see if that works.

Thank.  I tried that, and it still does not work.  I was wondering about this line:

row.textObj = display.newRetinaText( listRecs[idx].title, 0, 0, "Helvetica", 16 )   In Widgets 2.0, there is this sample: local function onRowUpdate( event ) local row = event.row print( "Row:", row.index, " is now visible" ) end -- Handle touches on the row local function onRowTouch( event ) local phase = event.phase if "press" == phase then print( "Touched row:", event.target.index ) end end

 

So, I was wondering if I should take out the [idx], because the index of the row appears built in to tableView.  If so, should the line be something like this:

row.textObj = display.newRetinaText( listRecs.index.title, 0, 0, “Helvetica”, 16 )

 

although, I did try that and it did not work.  It just seems that perhaps the index that appears built in may be the key issue.

Are you seeing anything in your tableView? Did you try printing idx to confirm that it’s set to what you expected?

Your rowRender function gets called when you insert Rows into the table.  It looks like your insert rows is inside your rowRender function.  This needs to be outside of that function and run by some other part of your code.

Thanks for your help.  I have moved the insert rows section to a separate function below rowRender.  I have called that function at the location where it was previously, as shown below.  And, I tried the print function below the insertRow function call.  I don’t know if that is the right place, because I get an error, that it is a nil value.  (I tried it with the x, and with idx from the code above.  I am totally confused, sorry)

function showRecords ()                local function onRowRender( event )             local phase = event.phase             local row = event.row             local rowGroup = event.view             local idx = event.index --add event.index to work with widgets 2.0             local color = {0,0,0}                          --widgets 2.0             db:insertRow{      -- changed list to db                            rowHeight=rowHeight,             isCategory=isCategory,             --rowColor=rowColor,             lineColor=lineColor,             }             -- title to display             row.textObj = display.newRetinaText( listRecs[idx].title, 0, 0, "Helvetica", 16 )             row.textObj:setTextColor( color )             row.textObj:setReferencePoint( display.CenterLeftReferencePoint )             row.textObj.x = 20             row.textObj.y = row.contentHeight \* 0.35  --was rowGroup                          -- add date to display             row.textObj2 = display.newRetinaText( listRecs[idx].date, 0, 0, "Helvetica", 12 )             row.textObj2:setTextColor( color )             row.textObj2:setReferencePoint( display.CenterLeftReferencePoint )             row.textObj2.x = 20             row.textObj2.y = row.contentHeight \* 0.65  --was rowGroup                          local function delRow( event )                 print("Delete hit: " .. tostring(event.target.id))                 local dbid = listRecs[event.target.id].id                 list:deleteRow(event.target.id) --remove from list widget                 table.remove(listRecs, event.target.id) --remove from table                 display.remove( detailGrp )                                  -- delete from database                 function deletData(id)                     local sql = "delete from News where id = " .. tostring(id)                     db:exec(sql)                 end                                  deleteData(dbid)                 --             end             row.delButton = widget.newButton{  -- check with Widgets 2.0 docs                 id = row.index,                 top = row.contentHeight \* 0.2,    --was rowGroup                 left = row.contentWidth - 90,  --was rowGroup                 default = "deletebtn.png",                 width = 64, height = 33,                 onRelease = delRow             }             row.delButton.alpha = 0                          if listRecs[idx].showDel == true then                 row.delButton.alpha = 1             end                          row:insert(row.delButton)  --was rowGroup             row:insert(row.textObj)  --was rowGroup             row:insert(row.textObj2)  --was rowGroup                      end --onRowRender                  local function rowListener( event )             local background = event.background             local row = event.row             local phase = event.phase                          if phase == "press" then                 print( "Pressed row: " .. row.index )                 background:setFillColor( 196, 255, 156, 255 )                                         elseif phase == "release" or phase == "tap" then                 print( "Tapped and/or Released row: " .. row.index )                 background:setFillColor( 196, 255, 156, 255 )                 row.reRender = true                                  -- go to new scene or add details to part of screen                 showDetails(row.index)                              elseif phase == "swipeLeft" then                 print( "Swiped Left row: " .. row.index )                 listRecs[row.index].showDel = true                 row.reRender = true                              elseif phase == "swipeRight" then                 print( "Swiped Right row: " .. row.index )                 listRecs[row.index].showDel = false                 display.remove( row.delButton )             end         end -- rowListener                       insertRow()         print ( listRecs[x].rowid, listRecs[x].date, listRecs[x].title ) end -- showRecords function insertRow()     for x = 1, #listRecs do         list:insertRow {             onRender = onRowRender,             listener = rowListener             }     end end

There isn’t enough to answer your question.  I don’t see where you create the list in the first place.  We can’t see the whole structure of the file.  Can you post more.  Please enclose the code in and tags (no spaces inside the brackets).

local function loadData()  --where to load from database     local sql = "select \* from News"     --local a = event.row.index --added for widgets 2.0              for a in db:nrows(sql) do             listRecs[#listRecs+1] =             --News[#News+1] =             {             rowid = a.rowid,             date = a.date,             title = a.title,             text = a.text             }     end          return dbNew      end

 

Before that I have

linePrinter function

setupDatabaseName function

setupInterface function

after  loadData
function I have

showDetails function

then the showRecords function which is included above

and last the insertRow function

If any of these other ones are necessary to narrow down the problem, let me know.  I only have a vague understanding sometimes of how these work together.  But, I am learning more.  First time programmer, and only been at it a month, so forgive me if I sound totally off.

 

Any ideas are appreciated!!

What version of Corona SDK are you using?  

Are you trying to use the Widget 1.0 opensource library?

Here is what a Widget 2.0 tableView should look like:

local tableView = widget.newTableView {     top = 0,     left = 0,     height = display.contentHeight,     width = display.contentWidth,     listener = tableViewListener,     onRowRender = onRowRender,     onRowTouch = onRowTouch, }   for i = 1, 30 do     local isCategory = false     local rowHeight = 40     local rowColor =     {         default = { 255, 255, 255 },     }     local lineColor = { 220, 220, 220 }     -- Insert the row into the tableView     tableView:insertRow     {         isCategory = isCategory,         rowHeight = rowHeight,         rowColor = rowColor,         lineColor = lineColor,     } end

Notice how the onRowRender is part of the tableView definition, not the insertRow call.

It looks like you’re using rowGroup = event.view and I think that’s one of the things that was deleted from widgets 2.0. Try just inserting the objects into row instead of rowGroup, and see if that works.

Thank.  I tried that, and it still does not work.  I was wondering about this line:

row.textObj = display.newRetinaText( listRecs[idx].title, 0, 0, "Helvetica", 16 )   In Widgets 2.0, there is this sample: local function onRowUpdate( event ) local row = event.row print( "Row:", row.index, " is now visible" ) end -- Handle touches on the row local function onRowTouch( event ) local phase = event.phase if "press" == phase then print( "Touched row:", event.target.index ) end end

 

So, I was wondering if I should take out the [idx], because the index of the row appears built in to tableView.  If so, should the line be something like this:

row.textObj = display.newRetinaText( listRecs.index.title, 0, 0, “Helvetica”, 16 )

 

although, I did try that and it did not work.  It just seems that perhaps the index that appears built in may be the key issue.

Are you seeing anything in your tableView? Did you try printing idx to confirm that it’s set to what you expected?

Your rowRender function gets called when you insert Rows into the table.  It looks like your insert rows is inside your rowRender function.  This needs to be outside of that function and run by some other part of your code.

Thanks for your help.  I have moved the insert rows section to a separate function below rowRender.  I have called that function at the location where it was previously, as shown below.  And, I tried the print function below the insertRow function call.  I don’t know if that is the right place, because I get an error, that it is a nil value.  (I tried it with the x, and with idx from the code above.  I am totally confused, sorry)

function showRecords ()                local function onRowRender( event )             local phase = event.phase             local row = event.row             local rowGroup = event.view             local idx = event.index --add event.index to work with widgets 2.0             local color = {0,0,0}                          --widgets 2.0             db:insertRow{      -- changed list to db                            rowHeight=rowHeight,             isCategory=isCategory,             --rowColor=rowColor,             lineColor=lineColor,             }             -- title to display             row.textObj = display.newRetinaText( listRecs[idx].title, 0, 0, "Helvetica", 16 )             row.textObj:setTextColor( color )             row.textObj:setReferencePoint( display.CenterLeftReferencePoint )             row.textObj.x = 20             row.textObj.y = row.contentHeight \* 0.35  --was rowGroup                          -- add date to display             row.textObj2 = display.newRetinaText( listRecs[idx].date, 0, 0, "Helvetica", 12 )             row.textObj2:setTextColor( color )             row.textObj2:setReferencePoint( display.CenterLeftReferencePoint )             row.textObj2.x = 20             row.textObj2.y = row.contentHeight \* 0.65  --was rowGroup                          local function delRow( event )                 print("Delete hit: " .. tostring(event.target.id))                 local dbid = listRecs[event.target.id].id                 list:deleteRow(event.target.id) --remove from list widget                 table.remove(listRecs, event.target.id) --remove from table                 display.remove( detailGrp )                                  -- delete from database                 function deletData(id)                     local sql = "delete from News where id = " .. tostring(id)                     db:exec(sql)                 end                                  deleteData(dbid)                 --             end             row.delButton = widget.newButton{  -- check with Widgets 2.0 docs                 id = row.index,                 top = row.contentHeight \* 0.2,    --was rowGroup                 left = row.contentWidth - 90,  --was rowGroup                 default = "deletebtn.png",                 width = 64, height = 33,                 onRelease = delRow             }             row.delButton.alpha = 0                          if listRecs[idx].showDel == true then                 row.delButton.alpha = 1             end                          row:insert(row.delButton)  --was rowGroup             row:insert(row.textObj)  --was rowGroup             row:insert(row.textObj2)  --was rowGroup                      end --onRowRender                  local function rowListener( event )             local background = event.background             local row = event.row             local phase = event.phase                          if phase == "press" then                 print( "Pressed row: " .. row.index )                 background:setFillColor( 196, 255, 156, 255 )                                         elseif phase == "release" or phase == "tap" then                 print( "Tapped and/or Released row: " .. row.index )                 background:setFillColor( 196, 255, 156, 255 )                 row.reRender = true                                  -- go to new scene or add details to part of screen                 showDetails(row.index)                              elseif phase == "swipeLeft" then                 print( "Swiped Left row: " .. row.index )                 listRecs[row.index].showDel = true                 row.reRender = true                              elseif phase == "swipeRight" then                 print( "Swiped Right row: " .. row.index )                 listRecs[row.index].showDel = false                 display.remove( row.delButton )             end         end -- rowListener                       insertRow()         print ( listRecs[x].rowid, listRecs[x].date, listRecs[x].title ) end -- showRecords function insertRow()     for x = 1, #listRecs do         list:insertRow {             onRender = onRowRender,             listener = rowListener             }     end end

There isn’t enough to answer your question.  I don’t see where you create the list in the first place.  We can’t see the whole structure of the file.  Can you post more.  Please enclose the code in and tags (no spaces inside the brackets).

local function loadData()  --where to load from database     local sql = "select \* from News"     --local a = event.row.index --added for widgets 2.0              for a in db:nrows(sql) do             listRecs[#listRecs+1] =             --News[#News+1] =             {             rowid = a.rowid,             date = a.date,             title = a.title,             text = a.text             }     end          return dbNew      end

 

Before that I have

linePrinter function

setupDatabaseName function

setupInterface function

after  loadData
function I have

showDetails function

then the showRecords function which is included above

and last the insertRow function

If any of these other ones are necessary to narrow down the problem, let me know.  I only have a vague understanding sometimes of how these work together.  But, I am learning more.  First time programmer, and only been at it a month, so forgive me if I sound totally off.

 

Any ideas are appreciated!!

What version of Corona SDK are you using?  

Are you trying to use the Widget 1.0 opensource library?

Here is what a Widget 2.0 tableView should look like:

local tableView = widget.newTableView {     top = 0,     left = 0,     height = display.contentHeight,     width = display.contentWidth,     listener = tableViewListener,     onRowRender = onRowRender,     onRowTouch = onRowTouch, }   for i = 1, 30 do     local isCategory = false     local rowHeight = 40     local rowColor =     {         default = { 255, 255, 255 },     }     local lineColor = { 220, 220, 220 }     -- Insert the row into the tableView     tableView:insertRow     {         isCategory = isCategory,         rowHeight = rowHeight,         rowColor = rowColor,         lineColor = lineColor,     } end

Notice how the onRowRender is part of the tableView definition, not the insertRow call.