Hello I would like to take a doubt, I have an app that scrapes the internet and I get a string in a variable and write it in a txt, so far so good, but when I try to display this string in a table, I always get nil, it doesn’t matter where in the code I include reading the string in the txt, and when looking inside the txt, the string is there! I realized that onRowRender executes the table line before reading the txt, even though this code is in show / did and the writing and reading of the string in the composer create.
Solar2D build 3590
macOS Catalina
If you could put together a sample project and upload it here, we might be able to help. It’s hard to say something without seeing some code.
local composer = require("composer")
local widget = require( "widget" )
local scene = composer.newScene()
local page
local bg
function scene:create( event )
local sceneGroup = self.view
background = display.newRect(sceneGroup, 0, 0, display.contentWidth, display.contentHeight)
background.anchorX = 0
background.anchorY = 0
background:setFillColor(1,0,0)
function networkListener( event )
if ( event.isError ) then
print( "Network error: ", event.response )
else
local dados = event.response
local pageLimited = string.match(dados,"<h4 class=\"sub\">......................")
local pageLimited2 = string.sub(pageLimited,18,38)
local pageLimited3 = string.gsub(pageLimited2,"\n","")
local dataFormatted = "Java: " .. pageLimited3
local path = system.pathForFile( "versions.txt", system.DocumentsDirectory )
local file, errorString = io.open( path, "w" )
if not file then
print( "File error: " .. errorString )
else
file:write( dataFormatted)
io.close( file )
end
file = nil
end
end
network.request( "https://www.java.com/pt_BR/download/", "GET", networkListener )
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
local path = system.pathForFile( "versions.txt", system.DocumentsDirectory )
local file, errorString = io.open( path, "r" )
if not file then
print( "File error: " .. errorString )
else
contents = file:read( "*a" )
io.close( file )
end
file = nil
local function onRowRender( event )
local row = event.row
local rowHeight = row.contentHeight
local rowWidth = row.contentWidth
local rowTitle = display.newText( row, contents, 0, 0, nil, 14 )
rowTitle:setFillColor( 0 )
rowTitle.anchorX = 0
rowTitle.x = 8
rowTitle.y = rowHeight * 0.5
end
local tableView = widget.newTableView(
{
left = 0,
top = display.contentCenterY + 20,
height = display.contentHeight - display.contentCenterY + 12,
width = display.contentWidth,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
listener = scrollListener
}
)
for i = 1, 1 do
tableView:insertRow{}
end
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
I’m sorry I didn’t understand what the problem is. It fails the first time because you check if the file exists and do nothing else so it takes a second run to work correctly. Also, it probably fails because you are not waiting for network request to complete before moving on. The code you posted does this in the second run.