So recently, I switched from using the widgets 1.0 to widgets 2.0 library to create a list view application.
I couldn’t seem to find the documentation I’m looking for but my main question is with the new widgets 2.0 (and new list view), how can I use the row rendering function to print from data{}? Is that where we would render the data in the first place? Specifically I am trying to render rows from data[i] = entry.title["$t"] and eventually others so I might have to do something like data[i].url= link.href and data[i].title = entry.title[“t”]
What I did was wrap most of the functions inside of the network listener so that all the data it pulls will be accessible. If there is a better way to do this please let me know.
That being said, here is my code below - note that I haven’t changed much from the sample code because I didn’t want to mess with anything further. I have been playing around with switching variables but had no luck so I went back to mostly using the stock sample code.
Thanks again!
display.setStatusBar( display.HiddenStatusBar ) -- Import the widget library local widget = require( "widget" ) --JSON for data local json = require "json" local dataURL = "http://www.google.com/calendar/feeds/csnfcl6h9k9qh769t6rs105pto@group.calendar.google.com/public/full?alt=json" --Set the background to white display.setDefault( "background", 255, 255, 255 ) --Create a group to hold our widgets & images local widgetGroup = display.newGroup() -- Create a background to go behind our tableView local background = display.newImage( widgetGroup, "bg.jpg", true ) -- The gradient used by the title bar local titleGradient = graphics.newGradient( { 255, 102, 0, 255 }, { 0, 0, 0, 255 }, "down" ) -- Create toolbar to go at the top of the screen local titleBar = display.newRect( 0, 0, display.contentWidth, 32 ) titleBar.y = display.statusBarHeight + ( titleBar.contentHeight \* 0.5 ) titleBar:setFillColor( titleGradient ) titleBar.y = display.screenOriginY + titleBar.contentHeight \* 0.5 -- create embossed text to go on toolbar local titleText = display.newEmbossedText( "Cogswell Calendar", 0, 0, native.systemFontBold, 20 ) titleText:setReferencePoint( display.CenterReferencePoint ) titleText:setTextColor( 255 ) titleText.x = 160 titleText.y = titleBar.y -- create a shadow underneath the titlebar (for a nice touch) local shadow = display.newImage( "shadow.png" ) shadow:setReferencePoint( display.TopLeftReferencePoint ) shadow.x, shadow.y = 0, titleBar.y + titleBar.contentHeight \* 0.5 shadow.xScale = 320 / shadow.contentWidth shadow.alpha = 0.45 --Text to show which item we selected local itemSelected = display.newText( "You selected item ", 0, 0, native.systemFontBold, 28 ) itemSelected.x = display.contentWidth + itemSelected.contentWidth \* 0.5 itemSelected.y = display.contentCenterY widgetGroup:insert( itemSelected ) -- Forward reference for our back button & tableview local backButton, list --Time to add some data... local data = {} local function newDataListener(event) if (event.isError) then print ("Error!") else print ( "RESPONSE: " .. event.response ) data = json.decode(event.response) print("success") print("Version: " .. data.version) print("Encoding: " .. data.encoding) for i,entry in ipairs(data.feed.entry) do --data[i] = {} data[i] = entry.title["$t"] print(data[i]) for i,link in ipairs(entry.link) do --data[i].url = link.href print(link.href) end end -- Handle row rendering local function onRowRender( event ) local phase = event.phase local row = event.row local rowTitle = display.newText( row,"This: " .. row.index, 0, 0, native.systemFontBold, 16 ) rowTitle.x = row.x - (row.contentWidth \* .4) + ( rowTitle.contentWidth \* 0.5 ) rowTitle.y = row.contentHeight \* 0.5 local rowArrow = display.newImage( row, "rowArrow.png", false ) rowArrow.x = row.x + ( row.contentWidth \* 0.4 ) - rowArrow.contentWidth rowArrow.y = row.contentHeight \* 0.5 end -- Hande row touch events local function onRowTouch( event ) local phase = event.phase local row = event.row if "press" == phase then print( "Pressed row: " .. row.index ) elseif "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( list, { x = - list.contentWidth, time = 400, transition = easing.outExpo } ) transition.to( itemSelected, { x = display.contentCenterX, time = 400, transition = easing.outExpo } ) transition.to( backButton, { alpha = 1, time = 400, transition = easing.outQuad } ) print( "Tapped and/or Released row: " .. row.index ) end end -- Create a tableView list = widget.newTableView { top = 38, width = 320, height = 448, hideBackground = true, maskFile = "mask-320x448.png", onRowRender = onRowRender, onRowTouch = onRowTouch, } --Insert widgets/images into a group widgetGroup:insert( list ) widgetGroup:insert( titleBar ) widgetGroup:insert( titleText ) widgetGroup:insert( shadow ) --Handle the back button release event local function onBackRelease() --Transition in the list, transition out the item selected text and the back button transition.to( list, { x = 0, time = 400, transition = easing.outExpo } ) transition.to( itemSelected, { x = display.contentWidth + itemSelected.contentWidth \* 0.5, time = 400, transition = easing.outExpo } ) transition.to( backButton, { alpha = 0, time = 400, transition = easing.outQuad } ) end --Create the back button backButton = widget.newButton { width = 298, height = 56, label = "Back", labelYOffset = - 1, onRelease = onBackRelease } backButton.alpha = 0 backButton.x = display.contentCenterX backButton.y = display.contentHeight - backButton.contentHeight widgetGroup:insert( backButton ) -- insert rows into list (tableView widget) for i = 1, 15 do list:insertRow { height = 72, rowColor = { default = { 255, 255, 255, 0 }, }, } end end end network.request ( dataURL, "GET", newDataListener )