JSON/Tableview: How to combine the two? How to "layout" tablerows?

Hi there, Corona regulars.

Before I get to the meat of the question, a bit of background: I have been trying to get into app programming as a hobby, coming from a non-programming career, and got the opportunity to make a small business app for a friend of mine. I took the challenge to get my feet wet with Corona, and so far have been more or less successful in teaching myself through tutorials and script examples. 

So far I managed to set up scripts to download JSON encoded data from a webserver and display it in the console, but I can not for the life of me figure out how I can display this data within a table on the device display. I have a working scrollable tableview widget, so that works, but maybe someone can point me to a tutorial or example on how to combine JSON and a tableview widget to display the data from the webserver on the display.

Maybe I am overlooking something, but is there also no way to “layout” for lack of a better word, the look of a table row and it’s contents? My example database has a couple of entries about companies, so what I would like to ultimately do is have the company name be in a larger font as a row title, the address in a smaller font size, a remotely loaded image next to the paragraph, etc.

Thanks in advance for any helpful replies, and sorry for being kind of obtuse about this, but I don’t know what else to search for that might bring me a step further.

A complete paid tutorial set can be found here : http://masteringcoronasdk.com/business-apps-using-corona-sdk-2/

Not related to the instructor. Just a happy customer. All the best.

I actually bought that, but I still can’t get my head around it. And I’ve been trying for weeks. :confused:

But thanks for the suggestion.

You’re most welcome. Why not post some code here so we can try and see where you’re stuck. All the best.

JSON is a data format that lets you read data in a standard way.  In Lua, this maps to table’s quite well.  You would take your JSON data string from where ever you’re getting it and then call the json.decode() function to assign the json data to a table.

Once you have a table of data, you can insert a row into the tableView for each record in the table.  There is a function called onRowRender() that can then read a row from your table and create the necessary display.newText’s and such to insert into the row, which is just a display.newGroup().

Hi everybody,

sorry for taking so much time to reply, but a friend of mine had an accident and I wasn’t on the computer much.

Thanks for trying to help me so far, I am just seriously stumped and can’t get my head around it.

Here is the code I am using so far, one example for how I get my results and one for how I create a table:

(Old:)

displayResults: http://pastebin.com/BNELz0Ne

tableView: http://pastebin.com/e1JQ46Vv

New:

displayTable.lua: http://pastebin.com/hjwtwi8X

Thanks in advance for sticking with me.

*Edited to add a slightly cleaner version of the code I am using so far.

Maybe you should read this blog post:  http://www.coronalabs.com/blog/2013/10/09/a-look-inside-a-sample-corona-powered-business-app/

And grab the code sample listed in it and see if that help you figure it out.

Rob

Thanks, Rob! Checking it out now.

So after trying to understand the blog post and the included code, I am unfortunately even more confused then before.

What I managed to get working so far is to download the data from my php file and story it into a txt file, after JSON decoding it.

I know that this works because it prints out the statements I choose to the console, but for some reason I keep getting a “nil” error whenever I try to display the same results in my table. I have a feeling that I am close to the solution, but still stumped here.

Here is the code if someone wants to point out what I am missing. Thank you in advance.

http://pastebin.com/5u8TiiCU

Can you post the error your getting along with the whole stack trace?  Without knowing where your getting the nil, its going to be really hard to look at the code and follow it.

Oh sorry, of course:

Runtime error
…na\shishan prealpha nov 2013\5\displayTableTest2.lua:33: attempt to index global ‘t’ (a nil value)
stack traceback:
        [C]: ?
        …na\shishan prealpha nov 2013\5\displayTableTest2.lua:33: in function
‘_onRowRender’
        ?: in function '_createRo

Though I see two issues.  One, the error you are reporting.  In onRowRender() how does it know what t is?  The table t is defined local to newDataListener.  This is what’s generating your error.

Secondly your inserting records into the tableView is happening before you get your data.  Remember network actions are asynchronous that is the call returns immediately and when the action completes the callback listener function is called when the data is ready.  The for i = 1, 10 do loop that holds your inserts needs to be more like for i = 1, #t do and should be where you are printing out those values in your network handler.

So scope t so that it exists so that onRowRender can see it and move your insert’s to the right place and you should be good to go.

Rob

Hey Rob, thanks a lot for your help so far, but I just can’t seem to get it right.

I tried making sure that the table functions only get called after the newDataListener ran its course, but I can’t figure out how to make the RowRenderer see my table and populate it right. Tried only printing the table to the console within the onRowRenderer but all it pastes is nil. Maybe I am way over my head on this. :confused:

I don’t think you’re in way over your head.  There are tons to learn about programming and you can’t get it all right away. 

An important part of learning to work with Lua is understanding basic programming concepts and a very important one when working with Lua is something called “Scope”.   Please watch this video and see if you understand your problem after watching it:

http://www.youtube.com/watch?v=2ATlcGP2zMY

Then come back and hopefully you will have your solution.

Rob

A complete paid tutorial set can be found here : http://masteringcoronasdk.com/business-apps-using-corona-sdk-2/

Not related to the instructor. Just a happy customer. All the best.

I actually bought that, but I still can’t get my head around it. And I’ve been trying for weeks. :confused:

But thanks for the suggestion.

You’re most welcome. Why not post some code here so we can try and see where you’re stuck. All the best.

JSON is a data format that lets you read data in a standard way.  In Lua, this maps to table’s quite well.  You would take your JSON data string from where ever you’re getting it and then call the json.decode() function to assign the json data to a table.

Once you have a table of data, you can insert a row into the tableView for each record in the table.  There is a function called onRowRender() that can then read a row from your table and create the necessary display.newText’s and such to insert into the row, which is just a display.newGroup().

Hi everybody,

sorry for taking so much time to reply, but a friend of mine had an accident and I wasn’t on the computer much.

Thanks for trying to help me so far, I am just seriously stumped and can’t get my head around it.

Here is the code I am using so far, one example for how I get my results and one for how I create a table:

(Old:)

displayResults: http://pastebin.com/BNELz0Ne

tableView: http://pastebin.com/e1JQ46Vv

New:

displayTable.lua: http://pastebin.com/hjwtwi8X

Thanks in advance for sticking with me.

*Edited to add a slightly cleaner version of the code I am using so far.

Maybe you should read this blog post:  http://www.coronalabs.com/blog/2013/10/09/a-look-inside-a-sample-corona-powered-business-app/

And grab the code sample listed in it and see if that help you figure it out.

Rob