Skip to content

pickerWheel:getValues() does not return selected values #209

@ARPGames

Description

@ARPGames

Function pickerWheel:getValues() returns the values of the updated pickerwheel EXCEPT if this function is executed in the event listener function triggered by the pickerwheel’s onValueSelected parameter. In the latter case, pickerWheel:getValues() returns the values of the pickerwheel before it was changed by the user.

However, the listener function’s (two) event values identify the pickerwheel’s column index (event.column) that was changed and the index of the value that was picked (event.row). So you can get the updated value in the event listener function with the following code, provided that you have created the table for the pickerwheel’s columns parameter (here: columnData) before/’above’ the listener function:

columnData[event.column].labels[event.row]

Code to illustrate this:

local pickerWheel -- forward declaration, so the pickerwheel values can be read in the pickerwheel's listener

local columnData =
{
{
align = "left",
width = 0.5 * display.contentWidth,
labelPadding = 20,
startIndex = 2,
labels = { "Hoodie", "Short Sleeve", "Long Sleeve", "Sweatshirt" }
},
{
align = "left",
width = 0.5 * display.contentWidth,
labelPadding = 10,
startIndex = 1,
labels = { "Dark Grey", "White", "Black", "Orange" }
}
}

local function onWheelChange ( event )
-- the event shows the correct wheel (key = 'column') and wheel position (key = 'row') after the change
for k,v in pairs( event ) do
print( 'Key: ' .. tostring( k ) .. '; value: ' .. tostring( v ) )
end

-- you can get the value that was updated with these indices
print( 'columnData[event.column].labels[event.row]: ' .. tostring( columnData[event.column].labels[event.row] ) )

-- the getValues() function returns the value(s) before the change
local scores = pickerWheel:getValues()
print( 'scores[event.column].value: ' .. tostring(scores[event.column].value) )

-- note that the pickerWheel event does not have an event.phase, so we can't distinguish between the start and end of the pickerwheel movement
print( 'event.phase: ' .. tostring(event.phase) )

end

-- Create the widget
pickerWheel = widget.newPickerWheel(
{
left = 0,
top = 0,
columns = columnData,
style = "resizable",
width = display.contentWidth,
rowHeight = 40,
fontSize = 18,
onValueSelected = onWheelChange,
})

I wonder whether this was meant to be so. It allows getting both the old and new value of the wheel that was changed by the user. If so, the documentation on object:getValues() should be adapted, because the sentence “Returns a table with the currently selected value/index of each column in the PickerWheelWidget.” is not entirely correct. A GOTCHA should be added for use of this function within the pickerwheel's event listener.

An elegant solution would be to add "began" and "ended" event.phase to the pickerwheel's onValueSelected event, but I suppose that could break or mess up older code. Because the current documentation suggests that the selected value is always read with getValues(), it may be harmless to existing code to ensure that it also reports the selected values in the wheel's listener function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions