我有一个列表对象,我正在通过 dataProvider 使用 addItem 向其中添加项目。
在将项目添加到列表之前,我想确保它不是重复的。我尝试在 dataProvider 上使用 indexOf 并返回 null。我已经尝试将它转换为一个数组并且它可以工作,但即使该元素存在于 dataProvider 中,它也总是返回 -1。
我能够使用的唯一方法似乎有点笨拙,我想知道是否有更好的方法可以在 dataProvider 中查找元素。
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" styleName="plain" applicationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.List;
[Bindable]
public var testListArray:Array;
public function init():void
{
testList.dataProvider.addItem('test');
testList.dataProvider.addItem('banana');
//search for element in dataProvider
if(testList.dataProvider.toString().indexOf('banana') > -1)
{
trace('found');
}
//search for element in dataProvider
if(testList.dataProvider.toString().indexOf('goat') === -1)
{
trace('not found');
}
}
]]>
</mx:Script>
<mx:List dataProvider="{testListArray}" x="260" y="204" id="testList" borderStyle="solid" borderColor="#000000"></mx:List>
</mx:Application>