【问题标题】:How to get a variable name that is created in a for loop如何获取在 for 循环中创建的变量名
【发布时间】:2019-10-04 06:14:13
【问题描述】:

基本上我有一个 for 循环,它根据数据库中的数据创建变量,然后我有一个事件侦听器,它也是基于 for 循环创建的,我想知道按下了哪些文本

我已经尝试了函数中的事件,为我的 row.name 等创建了一个变量。

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

函数看起来像

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

我希望能够跟踪按下了哪个学生姓名,但我找不到这样做的方法。我需要这个,以便我可以在数据库中跟踪它的名称,以便显示他们的信息

【问题讨论】:

  • 您可以使用event.xevent.y 来确定学生计数值,假设您的数据库的顺序与您进行显示时的顺序相同,您可以获得相同的行,使用一些东西像这样:stackoverflow.com/questions/16568/…
  • 我不需要知道学生的计数值,而是点击了哪些学生值,所以与数据库无关,而与变量无关
  • 哦,要做到这一点,只需使用self 访问文本对象并获取设置为row.name 的文本字段的值
  • local function studentscene(event) local name = event.target.text; ... end

标签: sql sqlite lua coronasdk scene


【解决方案1】:

你可以通过在 goto 函数中使用参数来添加它

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

例如这样做 params = {student="mohsen"} 并在场景中:显示功能这样做:

if event.params then

studen = event.params.name
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 2019-04-29
    • 2022-09-25
    • 2013-06-12
    • 1970-01-01
    相关资源
    最近更新 更多