gpt4 book ai didi

actionscript-3 - Flex arraycollection getitemindex 总是返回-1

转载 作者:行者123 更新时间:2023-12-04 08:26:17 25 4
gpt4 key购买 nike

我遇到了同样的问题。有人可以举个例子解释一下吗?

我的代码是:

var dataList:ArrayCollection = new ArrayCollection([{name:"alauddn"}, {name:"ansari"}]);

private function getItemInd(event:MouseEvent):void{

var item:Object = new Object();
item.name = "ansari";
var ias:int = dataList.getItemIndex(item);
Alert.show(ias.toString() + ": " + item.name);
}

但它返回“-1:

最佳答案

getItemIndex 不比较 arrayCollection 中的值。问题是 getItemIndex() 方法匹配精确的对象引用,而不是具有匹配属性的对象。

您应该改用这样的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="creationCompleteHandler(event)">

<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;

public var dataList:ArrayCollection = new ArrayCollection([{name:"alauddn"}, {name:"ansari"}]);

public function getItemIndexByProperty(array:ArrayCollection, property:String, value:String):Number
{
for (var i:Number = 0; i < array.length; i++)
{
var obj:Object = Object(array[i])
if (obj[property] == value)
return i;
}
return -1;

dataList.getItemIndex();
}

protected function creationCompleteHandler(event:FlexEvent):void
{
var ias:int = getItemIndexByProperty(dataList, "name", "ansari");
Alert.show(ias.toString() + " : " + dataList.getItemAt(ias).name);
}

]]>
</fx:Script>

</s:WindowedApplication>

关于actionscript-3 - Flex arraycollection getitemindex 总是返回-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8585300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com