Help how to use List View?

Hello

I’ve been having this problem with playing around with the list view. What I have been trying to do is when the user press on row 1 they go to scene 1, row 2 scene 2 like that. The problem is every time when I press row 1, 2, 3 etc any row it always takes me to scene 1. How do I make each row have an individual scene.

[code]

module(…, package.seeall)
function new()
local localGroup = display.newGroup()

– Abstract: List View sample app

– Version: 2.0

– Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license
– Copyright © 2010 Corona Labs Inc. All Rights Reserved.

– Demonstrates how to create a list view using the widget’s Table View Library.
– A list view is a collection of content organized in rows that the user
– can scroll up or down on touch. Tapping on each row can execute a
– custom function.

display.setStatusBar( display.HiddenStatusBar )

–import the widget library
local widget = require(“widget”)
widget.setTheme( “theme_ios” )

–Create a group to hold our widgets & images
local widgetGroup = display.newGroup()

–Create a background to display behind our tableView
local background = display.newImage( “bg.jpg”, true )
widgetGroup:insert( background )

– create a gradient for the top-half of the toolbar
local toolbarGradient = graphics.newGradient( {168, 181, 198, 255 }, {139, 157, 180, 255}, “down” )

– create toolbar to go at the top of the screen
local titleBar = widget.newTabBar{
gradient = toolbarGradient,
bottomFill = { 117, 139, 168, 255 },
height = 44
}
titleBar.y = display.screenOriginY + titleBar.contentHeight * 0.5

– create embossed text to go on toolbar
local titleText = display.newEmbossedText( “My List”, 0, 0, native.systemFontBold, 20 )
titleText:setReferencePoint( display.CenterReferencePoint )
titleText:setTextColor( 255 )
titleText.x = 160
titleText.y = titleBar.y

– create a shadow underneath the titlebar (for a nice touch)
local shadow = display.newImage( “shadow.png” )
shadow:setReferencePoint( display.TopLeftReferencePoint )
shadow.x, shadow.y = 0, titleBar.y + titleBar.contentHeight * 0.5
shadow.xScale = 320 / shadow.contentWidth
shadow.alpha = 0.45

–Text to show which item we selected
local itemSelected = display.newText( "You selected item ", 0, 0, native.systemFontBold, 28 )
itemSelected.x = display.contentWidth + itemSelected.contentWidth * 0.5
itemSelected.y = display.contentCenterY
widgetGroup:insert( itemSelected )

–Forward reference for our back button
local backButton

–Create Table view
local list = widget.newTableView{
width = 320,
height = 448,
bottomPadding = 8,
hideBackground = true,
maskFile = “mask-320x448.png”
}
list.y = titleBar.y + titleBar.contentHeight * 0.5
–Insert widgets/images into a group
widgetGroup:insert( list )
widgetGroup:insert( titleBar )
widgetGroup:insert( titleText )
widgetGroup:insert( shadow )
–Handle row rendering
local function onRowRender( event )
local row = event.row
local rowGroup = event.view
local label = "List item "
local color = 250

–Create the row’s text
row.textObj = display.newRetinaText( rowGroup, label … row.index, 0, 0, native.systemFontBold, 16 )
row.textObj:setTextColor( color )
row.textObj:setReferencePoint( display.CenterLeftReferencePoint )
row.textObj.x, row.textObj.y = 20, rowGroup.contentHeight * 0.5
rowGroup:insert( row.textObj )

–Create the row’s arrow
row.arrow = display.newImage( “rowArrow.png”, false )
row.arrow.x = rowGroup.contentWidth - row.arrow.contentWidth * 2
row.arrow.y = rowGroup.contentHeight * 0.5
rowGroup:insert( row.arrow )

end

–Handle the back button release event
local function onBackRelease()
–Transition in the list, transition out the item selected text and the back button
transition.to( list, { x = 0, time = 400, transition = easing.outExpo } )
transition.to( itemSelected, { x = display.contentWidth + itemSelected.contentWidth * 0.5, time = 400, transition = easing.outExpo } )
transition.to( backButton, { x = 60, alpha = 0, time = 400, transition = easing.outQuad } )
end
–Create the back button
backButton = widget.newButton{
style = “backSmall”,
label = “Back”,
yOffset = - 3,
onRelease = onBackRelease
}
backButton.alpha = 0
backButton.x = 60
backButton.y = titleBar.y
widgetGroup:insert( backButton )
–Hande row touch events
local function onRowTouch( event )
local row = event.row
local background = event.background

if event.phase == “press” then
print( "Pressed row: " … row.index )
background:setFillColor( 0, 110, 233, 255 )

elseif event.phase == “release” or event.phase == “tap” then
–Update the item selected text
–itemSelected.text = "You selected item " … row.index
director:changeScene(“menu2”)
–Transition out the list, transition in the item selected text and the back button
transition.to( list, { x = - list.contentWidth, time = 400, transition = easing.outExpo } )
transition.to( itemSelected, { x = display.contentCenterX, time = 400, transition = easing.outExpo } )
transition.to( backButton, { x = 40, alpha = 1, time = 400, transition = easing.outQuad } )

print( "Tapped and/or Released row: " … row.index )
background:setFillColor( 0, 110, 233, 255 )
row.reRender = true
end
end
– insert rows into list (tableView widget)
for i = 1, 20 do
list:insertRow{
height = 72,
rowColor = { 255, 255, 255, 0 },
onRender = onRowRender,
listener = onRowTouch
}

end

return localGroup
end

[/code] [import]uid: 17058 topic_id: 36149 reply_id: 336149[/import]

Change

[lua]

director:changeScene(“menu2”)

[/lua]

to

[lua]

director:changeScene(“scene”…row.index)

[/lua]

[import]uid: 93133 topic_id: 36149 reply_id: 143581[/import]

Thank for the help!!

EDIT:

Sorry about this I should just have mention everything. But also how do I give each row its individual text and image as well. [import]uid: 17058 topic_id: 36149 reply_id: 143583[/import]

You could alter the rowRender function to something like this:

[lua]

local rowText = {“This is the text for Row 1”,“Row 2 text”, “Here’s row 3”,“Don’t forget about row 4”}

local function onRowRender( event )
local row = event.row
local rowGroup = event.view
local label = "List item "
local color = 250

–Create the row’s text

local thisRowText

if rowText[row.index] ~= nil then
thisRowText = rowText[row.index]
else
thisRowText = “”
end

row.textObj = display.newRetinaText( rowGroup, thisRowText, 0, 0,native.systemFontBold, 16 )
row.textObj:setTextColor( color )
row.textObj:setReferencePoint( display.CenterLeftReferencePoint )
row.textObj.x, row.textObj.y = 20, rowGroup.contentHeight * 0.5
rowGroup:insert( row.textObj )

–Create the row’s arrow
row.arrow = display.newImage( “row”…row.index…".png",false )
row.arrow.x = rowGroup.contentWidth - row.arrow.contentWidth * 2
row.arrow.y = rowGroup.contentHeight * 0.5
rowGroup:insert( row.arrow )

end

[/lua]

This will display whatever you put in the rowText array for each row and load images called row1.png, row2.png etc. I’d also switch to using newImageRect so you can control the size of the images as you load them and take advantage of Corona’s automated scaling. [import]uid: 93133 topic_id: 36149 reply_id: 143593[/import]

Thanks!! [import]uid: 17058 topic_id: 36149 reply_id: 143627[/import]

Change

[lua]

director:changeScene(“menu2”)

[/lua]

to

[lua]

director:changeScene(“scene”…row.index)

[/lua]

[import]uid: 93133 topic_id: 36149 reply_id: 143581[/import]

Thank for the help!!

EDIT:

Sorry about this I should just have mention everything. But also how do I give each row its individual text and image as well. [import]uid: 17058 topic_id: 36149 reply_id: 143583[/import]

You could alter the rowRender function to something like this:

[lua]

local rowText = {“This is the text for Row 1”,“Row 2 text”, “Here’s row 3”,“Don’t forget about row 4”}

local function onRowRender( event )
local row = event.row
local rowGroup = event.view
local label = "List item "
local color = 250

–Create the row’s text

local thisRowText

if rowText[row.index] ~= nil then
thisRowText = rowText[row.index]
else
thisRowText = “”
end

row.textObj = display.newRetinaText( rowGroup, thisRowText, 0, 0,native.systemFontBold, 16 )
row.textObj:setTextColor( color )
row.textObj:setReferencePoint( display.CenterLeftReferencePoint )
row.textObj.x, row.textObj.y = 20, rowGroup.contentHeight * 0.5
rowGroup:insert( row.textObj )

–Create the row’s arrow
row.arrow = display.newImage( “row”…row.index…".png",false )
row.arrow.x = rowGroup.contentWidth - row.arrow.contentWidth * 2
row.arrow.y = rowGroup.contentHeight * 0.5
rowGroup:insert( row.arrow )

end

[/lua]

This will display whatever you put in the rowText array for each row and load images called row1.png, row2.png etc. I’d also switch to using newImageRect so you can control the size of the images as you load them and take advantage of Corona’s automated scaling. [import]uid: 93133 topic_id: 36149 reply_id: 143593[/import]

Thanks!! [import]uid: 17058 topic_id: 36149 reply_id: 143627[/import]

Change

[lua]

director:changeScene(“menu2”)

[/lua]

to

[lua]

director:changeScene(“scene”…row.index)

[/lua]

[import]uid: 93133 topic_id: 36149 reply_id: 143581[/import]

Thank for the help!!

EDIT:

Sorry about this I should just have mention everything. But also how do I give each row its individual text and image as well. [import]uid: 17058 topic_id: 36149 reply_id: 143583[/import]

You could alter the rowRender function to something like this:

[lua]

local rowText = {“This is the text for Row 1”,“Row 2 text”, “Here’s row 3”,“Don’t forget about row 4”}

local function onRowRender( event )
local row = event.row
local rowGroup = event.view
local label = "List item "
local color = 250

–Create the row’s text

local thisRowText

if rowText[row.index] ~= nil then
thisRowText = rowText[row.index]
else
thisRowText = “”
end

row.textObj = display.newRetinaText( rowGroup, thisRowText, 0, 0,native.systemFontBold, 16 )
row.textObj:setTextColor( color )
row.textObj:setReferencePoint( display.CenterLeftReferencePoint )
row.textObj.x, row.textObj.y = 20, rowGroup.contentHeight * 0.5
rowGroup:insert( row.textObj )

–Create the row’s arrow
row.arrow = display.newImage( “row”…row.index…".png",false )
row.arrow.x = rowGroup.contentWidth - row.arrow.contentWidth * 2
row.arrow.y = rowGroup.contentHeight * 0.5
rowGroup:insert( row.arrow )

end

[/lua]

This will display whatever you put in the rowText array for each row and load images called row1.png, row2.png etc. I’d also switch to using newImageRect so you can control the size of the images as you load them and take advantage of Corona’s automated scaling. [import]uid: 93133 topic_id: 36149 reply_id: 143593[/import]

Thanks!! [import]uid: 17058 topic_id: 36149 reply_id: 143627[/import]

Change

[lua]

director:changeScene(“menu2”)

[/lua]

to

[lua]

director:changeScene(“scene”…row.index)

[/lua]

[import]uid: 93133 topic_id: 36149 reply_id: 143581[/import]

Thank for the help!!

EDIT:

Sorry about this I should just have mention everything. But also how do I give each row its individual text and image as well. [import]uid: 17058 topic_id: 36149 reply_id: 143583[/import]

You could alter the rowRender function to something like this:

[lua]

local rowText = {“This is the text for Row 1”,“Row 2 text”, “Here’s row 3”,“Don’t forget about row 4”}

local function onRowRender( event )
local row = event.row
local rowGroup = event.view
local label = "List item "
local color = 250

–Create the row’s text

local thisRowText

if rowText[row.index] ~= nil then
thisRowText = rowText[row.index]
else
thisRowText = “”
end

row.textObj = display.newRetinaText( rowGroup, thisRowText, 0, 0,native.systemFontBold, 16 )
row.textObj:setTextColor( color )
row.textObj:setReferencePoint( display.CenterLeftReferencePoint )
row.textObj.x, row.textObj.y = 20, rowGroup.contentHeight * 0.5
rowGroup:insert( row.textObj )

–Create the row’s arrow
row.arrow = display.newImage( “row”…row.index…".png",false )
row.arrow.x = rowGroup.contentWidth - row.arrow.contentWidth * 2
row.arrow.y = rowGroup.contentHeight * 0.5
rowGroup:insert( row.arrow )

end

[/lua]

This will display whatever you put in the rowText array for each row and load images called row1.png, row2.png etc. I’d also switch to using newImageRect so you can control the size of the images as you load them and take advantage of Corona’s automated scaling. [import]uid: 93133 topic_id: 36149 reply_id: 143593[/import]

Thanks!! [import]uid: 17058 topic_id: 36149 reply_id: 143627[/import]