简体   繁体   中英

How to get a variable name that is created in a for loop

Basically I have a for loop that is creating variables based on data in a database, I then have an event listener that is also created based on the for loop and I want to know which text is pressed

Ive tried events in the function, creating a variable for my row.name etc.

for row in db:nrows( "SELECT * FROM Students WHERE Class = '"..class.."'" ) do
        print(row.Name)
        --track how many students there are 
        count = count+1
        --When displaying the names, put them in line, if they go below 1000 y level, move to the right and go down again
        ny = ny + 80
        if (ny == 1000) then
            nx = nx + 300
            ny = 280
        end
        -- Display students
        student[row] = display.newText( sceneGroup, row.Name, nx, ny, native.systemFont, 30 )
        --Make a button for every student in the row that goes to studentscene function
        student[row]:addEventListener( "tap", studentscene)
    end

The function then looks like

local function studentscene()
    composer.gotoScene( "student", { time=800, effect="crossFade" } )
end

I want to be able to track which student name was pressed, yet i cannot find a way to do so. I need this so I can track in the database which name it is so I can display their information

you can add it by using params in goto function

    `options = {
       effect = model,
       time = time,
       params = params,
    }
composer.gotoScene( nextScene , options )`

for example do this params = {student="mohsen"} and in scene:show function do this :

if event.params then

studen = event.params.name
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM