tableView error when loading remote image or when loading newimage

Hello! I’m new to developing with Corona/Lua and i’m trying to develop a list view using an xml that’s been exported from a magento shop. I used the tutorial on this page. When loading the image from a local folder in the list view it works fine but when i try to insert a downloaded image using loadRemoteImage i get an error saying

[text]ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’
stack traceback:
[C]: ?
[C]: in function ‘insert’[/text]

I tried to download the remote image while reading the xml file and then use the image again with newImage in the listview but it won’t show. When i comment out the loadRemoteImage it works fine.

My code;

[lua]local tableView = require(“tableView”)

local ui = require(“ui”)

local xml = require(“xmlSimple”).newParser()

local parsedXml = xml:loadFile(“file.xml”, base)
local data = {}

function removecdata(val)

local i = 1
local cdata_start,cdata_start2 = val:find(" if (cdata_start) then

abs = i+cdata_start2

cdata_end,cdata_end2 = val:find("%]%]>",i)
i = cdata_end2

val = val:sub(abs,cdata_end-1)
end

return val
end

–XML to ARRAY
for y = 1, 10, 1 do

data[y] = {}
data[y].title = removecdata(parsedXml.rss.channel.item[y].title:value())
data[y].subtitle = removecdata(parsedXml.rss.channel.item[y].description:value())
data[y].link = removecdata(parsedXml.rss.channel.item[y].link:value())
data[y].image = removecdata(parsedXml.rss.channel.item[y]:children()[9]:value())

data[y].imgTitle = “img”…y…".jpg"

–display.loadRemoteImage(data[y].image, “GET”, networkListener, data[y].imgTitle, system.DocumentsDirectory)

end

local function networkListener( event )
if ( event.isError ) then
print ( “Network error - download failed” )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
end

print ( "RESPONSE: " … event.response )
end

local g = display.newGroup()

– ARRAY to TABLE
local myList = tableView.newList{
data=data,
default=“listItemBg.png”,
backgroundColor={255,2552,255},
callback = function( row )

–local img = display.newImage(row.imgTitle, system.DocumentsDirectory,0,0)
local img = display.loadRemoteImage(row.image, “GET”, networkListener, row.imgTitle, system.DocumentsDirectory)
g:insert(img)
img.x = math.floor(img.width*0.5 + 6)
img.y = math.floor(img.height*0.5)

local title = display.newText( row.title, 0, 0, native.systemFontBold, 14 )
title:setTextColor(0,0,0)
g:insert(title)
title.x = title.width*0.5 + img.width + 6
title.y = 30

local subtitle = display.newText( row.subtitle, 0, 0, native.systemFont, 12 )
subtitle:setTextColor(80,80,80)
g:insert(subtitle)
subtitle.x = subtitle.width*0.5 + img.width + 6
subtitle.y = title.y + title.height + 6

return g
end
}[/lua]

I hope someone can help me…

Thanks in advance! [import]uid: 199479 topic_id: 33671 reply_id: 333671[/import]

What version of Corona are you using?

[import]uid: 84637 topic_id: 33671 reply_id: 133857[/import]

Hi,
my version is 2.0.0, build is 2012.971

but… i figured out what my problem is (haven’t got the solution though).

While looping through the xml, the images are beeing downloaded with loadRemoteImage. When the listview is filling the images are still downloading, so display.newImage can’t show anything. When i put a timer.performWithDelay in between it all works. Problem i now have is when to know when the downloading of the images is finished. [import]uid: 199479 topic_id: 33671 reply_id: 133866[/import]

@youri3 – sounds pretty much just like your issue:

http://web-c1.anscamobile.com/forum/2012/11/27/displayloadremoteimage-tableview
[import]uid: 79933 topic_id: 33671 reply_id: 133874[/import]

I would recommend switching your tableView to to the one in the supported widget.* library. It has an onRender function and if you put your loadRemoteImage() call in the rendering function you can insert the image in event.target into the row and manage that better.
[import]uid: 199310 topic_id: 33671 reply_id: 133953[/import]

Thanks for the replies. I will switch it to the correct widget from the library! I got it working btw with the tableview;

[lua]local tableView = require(“tableView”)

local ui = require(“ui”)

local xml = require(“xmlSimple”).newParser()

local parsedXml = xml:loadFile(“sample.xml”, base)
local data = {}

function removecdata(val)

local i = 1
local cdata_start,cdata_start2 = val:find(" if (cdata_start) then

abs = i+cdata_start2
–print(“Found CDATA start tag”, abs)

cdata_end,cdata_end2 = val:find("%]%]>",i)
–print(“Found CDATA end tag”, cdata_end2)
i = cdata_end2

val = val:sub(abs,cdata_end-1)
end

return val
end

–XML to ARRAY
for y = 1, 10, 1 do

data[y] = {}
data[y].title = removecdata(parsedXml.rss.channel.item[y].title:value())
data[y].subtitle = removecdata(parsedXml.rss.channel.item[y].description:value())
data[y].link = removecdata(parsedXml.rss.channel.item[y].link:value())
data[y].image = removecdata(parsedXml.rss.channel.item[y]:children()[9]:value())

data[y].imgTitle = “beeld”…y…".jpg"

end

function makeList()
– ARRAY to TABLE
local myList = tableView.newList{
data=data,
default=“listItemBg.png”,
backgroundColor={255,2552,255},
callback = function( row )

local g = display.newGroup()
local img

local function networkListenerIm( event )
if ( event.isError ) then
print ( “Network error - download failed” )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
img = event.target
g:insert(img)
end
print ( "RESPONSE: " … event.response )
end

display.loadRemoteImage(row.image, “GET”, networkListenerIm, row.imgTitle, system.DocumentsDirectory)

–img.x = math.floor(img.width*0.5 + 6)
–img.y = math.floor(img.height*0.5)

local title = display.newText( row.title, 0, 0, native.systemFontBold, 14 )
title:setTextColor(0,0,0)
g:insert(title)
–title.x = title.width*0.5 + img.width + 6
title.x = title.width*0.5 + 6
title.y = 30

local subtitle = display.newText( row.subtitle, 0, 0, native.systemFont, 12 )
subtitle:setTextColor(80,80,80)
g:insert(subtitle)
–subtitle.x = subtitle.width*0.5 + img.width + 6
subtitle.x = subtitle.width*0.5 + 6
subtitle.y = title.y + title.height + 6

return g
end
}
end

timer.performWithDelay(100,makeList)[/lua] [import]uid: 199479 topic_id: 33671 reply_id: 134024[/import]

What version of Corona are you using?

[import]uid: 84637 topic_id: 33671 reply_id: 133857[/import]

Hi,
my version is 2.0.0, build is 2012.971

but… i figured out what my problem is (haven’t got the solution though).

While looping through the xml, the images are beeing downloaded with loadRemoteImage. When the listview is filling the images are still downloading, so display.newImage can’t show anything. When i put a timer.performWithDelay in between it all works. Problem i now have is when to know when the downloading of the images is finished. [import]uid: 199479 topic_id: 33671 reply_id: 133866[/import]

@youri3 – sounds pretty much just like your issue:

http://web-c1.anscamobile.com/forum/2012/11/27/displayloadremoteimage-tableview
[import]uid: 79933 topic_id: 33671 reply_id: 133874[/import]

I would recommend switching your tableView to to the one in the supported widget.* library. It has an onRender function and if you put your loadRemoteImage() call in the rendering function you can insert the image in event.target into the row and manage that better.
[import]uid: 199310 topic_id: 33671 reply_id: 133953[/import]

Thanks for the replies. I will switch it to the correct widget from the library! I got it working btw with the tableview;

[lua]local tableView = require(“tableView”)

local ui = require(“ui”)

local xml = require(“xmlSimple”).newParser()

local parsedXml = xml:loadFile(“sample.xml”, base)
local data = {}

function removecdata(val)

local i = 1
local cdata_start,cdata_start2 = val:find(" if (cdata_start) then

abs = i+cdata_start2
–print(“Found CDATA start tag”, abs)

cdata_end,cdata_end2 = val:find("%]%]>",i)
–print(“Found CDATA end tag”, cdata_end2)
i = cdata_end2

val = val:sub(abs,cdata_end-1)
end

return val
end

–XML to ARRAY
for y = 1, 10, 1 do

data[y] = {}
data[y].title = removecdata(parsedXml.rss.channel.item[y].title:value())
data[y].subtitle = removecdata(parsedXml.rss.channel.item[y].description:value())
data[y].link = removecdata(parsedXml.rss.channel.item[y].link:value())
data[y].image = removecdata(parsedXml.rss.channel.item[y]:children()[9]:value())

data[y].imgTitle = “beeld”…y…".jpg"

end

function makeList()
– ARRAY to TABLE
local myList = tableView.newList{
data=data,
default=“listItemBg.png”,
backgroundColor={255,2552,255},
callback = function( row )

local g = display.newGroup()
local img

local function networkListenerIm( event )
if ( event.isError ) then
print ( “Network error - download failed” )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
img = event.target
g:insert(img)
end
print ( "RESPONSE: " … event.response )
end

display.loadRemoteImage(row.image, “GET”, networkListenerIm, row.imgTitle, system.DocumentsDirectory)

–img.x = math.floor(img.width*0.5 + 6)
–img.y = math.floor(img.height*0.5)

local title = display.newText( row.title, 0, 0, native.systemFontBold, 14 )
title:setTextColor(0,0,0)
g:insert(title)
–title.x = title.width*0.5 + img.width + 6
title.x = title.width*0.5 + 6
title.y = 30

local subtitle = display.newText( row.subtitle, 0, 0, native.systemFont, 12 )
subtitle:setTextColor(80,80,80)
g:insert(subtitle)
–subtitle.x = subtitle.width*0.5 + img.width + 6
subtitle.x = subtitle.width*0.5 + 6
subtitle.y = title.y + title.height + 6

return g
end
}
end

timer.performWithDelay(100,makeList)[/lua] [import]uid: 199479 topic_id: 33671 reply_id: 134024[/import]