Displaying data from Rest Api

Hey guys i am kinda new to corona and i am trying to build an app where i get a list of posts from a rest api and display the names of the posts in a list

but i am facing problems displaying the data i am using a foreach loop to display a new text for every item from the rest api but all the items i am listing is being position in the same y position and all the texts are overlaying 

how can i position each post name from the rest api on a seperate line, ie changing the y position of each posts name when being display 

Thanks a lot 

I usually use one of two patterns there are probably better ideas and hopefully, they’ll post them:

  1. If I know the limit of the size of the array and that limit is small. I just vary the y axel by a multiple of the height. 

    item.y = math.floor(display.contentHeight/16) * 3

  2. Otherwise, I use the https://docs.coronalabs.com/api/type/TableViewWidget/index.html. I have crude implementation as part of my example here:

https://github.com/agramonte/gamesparkscoronaleaderboard

I would agree with @agramonte. This sounds like a use for a widget.newTableView(). If you end up with more items that fit on the screen, it will handle the scrolling for you.

But generally if I want to position something top to bottom like that I will take the Y position of the previous object and add the height of the object to it.  Something like:

local textObjects = {} local Y = 50 -- arbitrary starting point for i = 1, numberOfTextObjectsToCreate do      textObjects[i] = display.newText( ... ) -- not a real ... but the parameters you need to pass.      textObjects[i]. y = Y      Y = Y + 25 -- if 25 is the right amount of height or ...      Y = Y + textObjects[i].height -- or if your objects are of variable height      if i \> 1 then           Y = Y + textObjects[i -1].height / 2 + textObjects[i].height / 2      end end

Note, there are three different Y calculations. Pick the one that works best for you, don’t use all three.

Rob

thanks alot guys got it 

I usually use one of two patterns there are probably better ideas and hopefully, they’ll post them:

  1. If I know the limit of the size of the array and that limit is small. I just vary the y axel by a multiple of the height. 

    item.y = math.floor(display.contentHeight/16) * 3

  2. Otherwise, I use the https://docs.coronalabs.com/api/type/TableViewWidget/index.html. I have crude implementation as part of my example here:

https://github.com/agramonte/gamesparkscoronaleaderboard

I would agree with @agramonte. This sounds like a use for a widget.newTableView(). If you end up with more items that fit on the screen, it will handle the scrolling for you.

But generally if I want to position something top to bottom like that I will take the Y position of the previous object and add the height of the object to it.  Something like:

local textObjects = {} local Y = 50 -- arbitrary starting point for i = 1, numberOfTextObjectsToCreate do      textObjects[i] = display.newText( ... ) -- not a real ... but the parameters you need to pass.      textObjects[i]. y = Y      Y = Y + 25 -- if 25 is the right amount of height or ...      Y = Y + textObjects[i].height -- or if your objects are of variable height      if i \> 1 then           Y = Y + textObjects[i -1].height / 2 + textObjects[i].height / 2      end end

Note, there are three different Y calculations. Pick the one that works best for you, don’t use all three.

Rob

thanks alot guys got it