I REALLY need help. I have an app that once the userInput has been set, a network.request is sent. I Understand how to create a network request and also how to create a Table View. I just dont understand how to put the information from the network request (JSON) and insert the information I want into rows.
Im very confused about the for loop. not sure where to place it.
please help
local composer = require( “composer” )
local scene = composer.newScene()
local widget = require(“widget”)
local json = require(“json”)
local search = require “menu”
– All code outside of the listener functions will only be executed ONCE unless “composer.removeScene()” is called.
font = “Helvetica” or native.systemFont
centerX = display.contentCenterX
centerY = display.contentCenterY
_W = display.contentWidth
_H = display.contentHeight
local resultsTable
function onRowRender(event)
local row = event.row
local id = row.index
row.bg = display.newRect( 0, 0, _W, 60 )
row.bg.anchorX = 0
row.bg.anchorY = 0
row.bg:setFillColor ( 1, 1, 1 )
row:insert(row.bg)
if event.row.params then
row.nameText = display.newText( myData[id].name, 12, 0, font, 18 )
row.nameText.anchorX = 0
row.nameText.anchorY = 0.5
row.nameText:setFillColor ( 0)
row.nameText.y = 20
row.nameText.x = 42
row:insert( row.nameText )
end
return true
end
– “scene:create()”
function scene:create( event )
local function dataListener(event)
–if network event is error show alert
if event.isError then
print(“Network Error”)
else
local myData = {}
myData = json.decode(event.response)
for i = 1, #myData do
resultsTable:insertRow{
rowHeight = 50,
isCategory = false,
rowColor = {1, 1, 1},
}
end
end
return true
end
resultsTable = widget.newTableView {
top = display.statusBarHeight,
height = _H,
width = _W,
onRowRender = onRowRender,
}
network.request ( URL, “GET”, dataListener)
end
– “scene:show()”
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == “did” ) then
– Called when the scene is now on screen.
– Insert code here to make the scene come alive.
– Example: start timers, begin animation, play audio, etc.
end
end
– “scene:hide()”
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Called when the scene is on screen (but is about to go off screen).
– Insert code here to “pause” the scene.
– Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == “did” ) then
– Called immediately after scene goes off screen.
end
end
– “scene:destroy()”
function scene:destroy( event )
local sceneGroup = self.view
– Called prior to the removal of scene’s view (“sceneGroup”).
– Insert code here to clean up the scene.
– Example: remove display objects, save state, etc.
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene