Hi,
I started coding with Lua and Corona yesterday and have run in to a problem with tables. I am using one of the examples from the Ansca blog (Gilbert’s Coffee application), except that I am loading my data from a database. I use a server-side script to return JSON-formatted results, which I then gather using a HTTP request, like this:
responseJSon = http.request("http://www.mysite.com/getData.asp")
jSon\_result = json.decode(responseJSon)
I then set up an array and read the JSON data in to it. I have “name”, “unitprice” and “image” fields in my sample JSON data, which I can access as simple array elements in Lua:
...
local data = {}
for i=1, jSon\_result.recordcount do
data[i] = {}
data[i].title = jSon\_result.data.name[i]
data[i].subtitle = jSon\_result.data.unitprice[i]
local path = system.pathForFile(jSon\_result.data.image1[i], system.DocumentsDirectory)
thisFile = io.open(path, "w+b")
imgUrl = http.request {
url = "http://www.mysite.com/images/" .. jSon\_result.data.image1[i],
sink = ltn12.sink.file(thisFile),
}
data[i].image = jSon\_result.data.image1[i]
table.insert(data, data[i])
end
As you can see, I loop over the JSON records, the total of which is kept in the “recordcount” variable. I also display the image, the URL of which is passed in my data as field “image”. All straightforward stuff.
Finally, I output the data using a tableView as follows:
myList = tableView.newList {
data=data,
default="listItemBg.png",
top=topBoundary,
bottom=bottomBoundary,
callback = function( row )
local g = display.newGroup()
local img = display.newImage(row.image, system.DocumentsDirectory, 0, 0)
g:insert(img)
img:scale(0.4, 0.4)
img.x = 150
img.y = 44
local title = display.newText(row.title, 0, 0, "georgia", 10)
title:setReferencePoint(display.CenterLeftReferencePoint)
title:setTextColor(0, 0, 0)
g:insert(title)
title.x = 6
title.y = 10
local subtitle = display.newText(row.subtitle, 0, 0, "georgia", 10)
subtitle:setReferencePoint(display.CenterLeftReferencePoint)
subtitle:setTextColor(80,80,80)
g:insert(subtitle)
subtitle.x = 6
subtitle.y = 25
return g
end
}
This all works and renders fine in Corona, but the wierd issue I’m having is that when I preview my application, the final row of data is always duplicated. So I get two identical final table rows, with duplicate image and other data.
I have checked the JSON data and all is ok there. The issue seems to be happening in the table.insert() stage since that will run 11 times if there are 10 records, or 6 times if there are 5 records, and so forth. By putting in print() statements to debug, the tableView.newList callback code seems to always get called one more time than is necessary (but the loop iterates correctly.) The loop structure is correct, it starts at 1 and goes up to the number of items in “recordset” (both correctly verified when debugging.)
Is this a bug in Corona, or Lua? Can anyone shed some light? I’ve used tableView.lua v1.4 and v1.8 and also tableViewXL.lua and all do the same thing.
I would also like to know if there is a better way to get JSON data directly into a tableView without using the intermediate array.
Help greatly appreciated. [import]uid: 116752 topic_id: 20231 reply_id: 320231[/import]
[import]uid: 116752 topic_id: 20231 reply_id: 79147[/import]
[import]uid: 84637 topic_id: 20231 reply_id: 79198[/import]