How can add item to listview in Cocos2dx lua


#1

I am removing/inserting item to listview in local function
But I get error message “LUA ERROR: ASSERT FAILED ON LUA EXECUTE: The object should not be nullptr”

this is my code

local state = true
local function pushItem()
state = not state
if state ~= true then
listview:insertCustomItem(subwidget, self:getIndex(widget) + 1)
else
listview:removeItem(self:getIndex(subwidget))
end
end
self:runAction(cc.Sequence:create(cc.DelayTime:create(1), cc.CallFunc:create(pushItem)))

So if you have many experiences, Please help me
Best Regards


#2

Maybe , subwidget needs retain. If it doesn’t do, it will be removed by main ui thread immediately.

ex.
local subwidget = ccui.Layout:create()
subwidget:retain()

listview:insertCustomItem(subwidget, self:getIndex(widget) + 1)


#3

Ok, good
It works very well
Thanks