hi
as you know corona sdk does not have DropDownList widget By Default,
so im trying to make one , (code Below)
the Problem is the ans Table is not uniq for each instance and all instances can access this field.
actualy im a c# programmer , and after many practices, readings , and cod Play i could not undrestand the OOP Concepts in Corona SDK/Lua .
note: in main.lua we have 2 diffrent DropDownList , But in ‘test’ event we just call the ddl1:getSelectedItem() , the problem is this function returns the values of last ddl that has been changed(like ddl2)
–=== main.lua=====
local m=require(“dropDown”)
local list1={“ali”,“hasan”,“reza”,“taghi”}
local list2={“M”,“Z”,“K”,“S”,“T”}
local ddl1=m.new(“ddl1” , centerX,centerY-200 ,150,45,list1)
local ddl2=m.new(“ddl2” , centerX,centerY+100 ,150,45,list2)
ddl1:create()
ddl2:create()
local function test(event)
s= ddl1:getSelectedItem()
print(s.id)
print(s.txt)
end
local ss = display.newText( “tap here see result”, centerX, centerY, “helvetica”, 15 )
ss:addEventListener ( “tap”, test)
– === dropDown.lua ====
local widget = require( “widget” )
local ddl = {}
local ddl_mt = { __index = ddl } – metatable
– PRIVATE FUNCTIONS
local ans={id,txt}
local function comboBox(cx,cy,cWidth,cHeight,cList)
local combo = display.newGroup()
local isListShowing=false
local tableView --List TableView
local selText --Selected Text
–local ans={id,txt}
local function showList(event)
if isListShowing==true then
tableView.alpha = 0
isListShowing=false
return
end
isListShowing=true
local function onRowRender( event )
local phase = event.phase
local row = event.row
local groupContentHeight = row.contentHeight
local params = event.row.params
if ( event.row.params ) then
local rowTitle = display.newText( row, params.txt…" "…params.id, 0, 0, nil, 14 )
rowTitle.x = 10
rowTitle.anchorX = 0
rowTitle.y = groupContentHeight /2
rowTitle:setFillColor( 0, 0, 0 )
rowTitle.id=params.id
end
end --===>> End Of OnRowRender
local function onRowTouch( event )
local phase = event.phase
local row = event.target
if phase==“release” then
–local itemSelected = "You selected item " … row.index
–print(itemSelected)
selText.text=cList[row.index]
display.remove( tableView )
isListShowing=false
end
ans.id=cList[row.index]
ans.txt=row.index
end --== end of OnRowTouch()
tableView = widget.newTableView
{
x=cx,
y=cy+150/2+cHeight/2,
width = cWidth,
height = 150,
isBounceEnabled=false,
listener = tableViewListener,
onRowRender = onRowRender,
–onRowUpdate = onRowUpdate,
onRowTouch = onRowTouch,
}
for i = 1, #cList do
local isCategory = false
local rowHeight = 50
local rowColor = { default = { 1, 1, 1 }, over = { 30/255, 144/255, 1 }, }
local lineColor = { 220/255, 220/255, 220/255 }
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
params={
txt=cList[i],
id=100+i
}
}
end ---- ====== > End Of For Loop
end --==> end of showList()
local box=display.newRect( cx, cy, cWidth, cHeight )
box:setFillColor(253/255, 255/255, 208/255)
box.strokeWidth = 1
box:setStrokeColor(1,0,0)
combo:insert(box)
local btn=widget.newButton{
x = cx+cWidth/2 -5 ,–box.width-100, – chapo rast
y = cy, – bala va paeeen
height=box.height,
width=15,
id = “btn”,
label = “>”,
}
combo:insert(btn)
selText=display.newText( “ظطزظطزظطزظط”, cx, cy, “helvetica”, 18 )
selText:setFillColor(0,1,0)
combo:insert(selText)
combo:addEventListener ( “tap”,showList )
–return ans
end – ==> end of combobox creation
– PUBLIC FUNCTIONS
function ddl.new( cName , cx ,cy,cWidth,cHeight,cList ) – constructor
local newDdl = {
cx = cx , – name or “Unnamed”,
cy = cy , – ageInYears or 2,
cWidth=cWidth ,
cHeight=cHeight ,
cList=cList ,
cName=cName ,
}
return setmetatable( newDdl, ddl_mt )
end
function ddl:create()
comboBox(self.cx ,self.cy ,self.cWidth ,self.cHeight ,self.cList)
end
function ddl:getSelectedItem()
return ans
end
return ddl