Scrolling list views, amendment, thanks for help

I try to amend the scrolling list views example, based on the top tutorial posted by Gilbert "CoffeDemo in sourceCode).
My goal: firts, the list is empty and an “addbutton”, when pressed should add a new row (then I will add an “editButton” to delete rows).

My code below adds effectively rows but is not completely correct; when button pressed:

  • 1st time: row1 appears: ok
  • 2nd time: 2nd row appears, but behind row1, a second row1 appears
  • 3rd time: 3rd row appears, but behind row1, a second row1 appears and behind row2, a second row2.
  • etc.

Thanks to point my mistake.

[lua]–FUNCTION TO INCREMENT THE LIST WHEN BUTTON PRESSED
local i = 1
local function addBtnRelease ( event )

data[i] = {}
data[i].title = "Iced Coffee " … i
data[i].subtitle = "Chilled coffee with ice " … i
data[i].image = “coffee2.png”
i = i + 1

– create the list of items
myList = tableView.newList{
data=data,
default=“listItemBg.png”,
–default=“listItemBg_white.png”,
over=“listItemBg_over.png”,
onRelease=listButtonRelease,
top=topBoundary,
bottom=bottomBoundary,
backgroundColor={ 255, 255, 255 }, --commented this out because we’re going to add it down below

callback = function( row )
local g = display.newGroup()

local img = display.newImage(row.image)
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)
–title:setTextColor(255, 255, 255)
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)
–subtitle:setTextColor(180,180,180)
g:insert(subtitle)
subtitle.x = subtitle.width*0.5 + img.width + 6
subtitle.y = title.y + title.height + 6

return g
end

}

end[/lua] [import]uid: 8970 topic_id: 4890 reply_id: 304890[/import]

your creating a *new* list every you press the button, so the first time you get a list of 1, then a list of 2 items gets added on top of it, then a list of 3 items gets added on top of that etc

[lua]local function addBtnRelease ( event )


myList = tableView.newList{[/lua]

you could try removing myList first and then recreating it from the new “data” array

there’s probably another way to append but i’ve not tried this example before [import]uid: 6645 topic_id: 4890 reply_id: 15759[/import]

Thanks++, successful! I just added after the declaration of addBtnRelease:

[lua]if i > 1 then
myList:removeSelf()
end[/lua] [import]uid: 8970 topic_id: 4890 reply_id: 15765[/import]

Regarding the example above, two questions. If someone can show me the way, thanks:

  • I would like to add a new raw at the top of the list instead at the bottom. How to implement that (very few examples in documentation…).

  • a button, when pressed sould set an objet of a raw visible (not visible by default). How to implement a logic of “title.isVisible = false / true” when the concerned object is part of a raw in a table.
    PS. A “CoffeDemo” version with a dynamic structure (possibility to add or delete raws and save and restore them, would be a very useful template… [import]uid: 8970 topic_id: 4890 reply_id: 16556[/import]

Hey,

I’m working on an app that does just that and I’m looking for some feedback/beta testers. If your interested send me an email to jeremyapplebaum12 (at) gmail dot com.

Thanks. [import]uid: 23649 topic_id: 4890 reply_id: 54769[/import]