apache-flex - 弹性3 : Can anybody see why this dictionary isn't working?

标签 apache-flex dictionary

因此,在我的主 mxml 中,我定义了一个变量,如下所示:

[Bindable] public var studentsListDict:Dictionary = new Dictionary;

我还导入了以下内容:

import flash.utils.Dictionary;

然后我有一个导入 XML 文件的 HTTPService:

<mx:HTTPService id="studentsHttp" url="students.xml" resultFormat="e4x" makeObjectsBindable="true" result="createStudentsCollection(event)" />

createStudentsCollection函数如下:

private function createStudentsCollection(e:ResultEvent):void
{
    var xmlList:XMLList = XML(e.result).student;
    var dupString:String = "|";
    var tempArray:Array = new Array;
    studentsListDict = new Dictionary;

    for (var i:int = 0; i < xmlList.length(); i++)
    {
        if (dupString.indexOf(String("|" + xmlList[i].name) + "|") == -1)
        {
            tempArray = new Array;
            tempArray[0] = xmlList[i].name.@id;
            tempArray[1] = xmlList[i].name;
            tempArray[2] = xmlList[i].year;
            tempArray[3] = xmlList[i].track;

            studentsListAC.addItem(tempArray);
            studentsListDict[tempArray[0]] = tempArray;
            dupString += "|" + xmlList[i].name + "|";

            getLen(studentsListDict);
        }
    }
}

然后,为了确保项目正确放入字典中,我有以下功能:

public static function getLen(d:Dictionary):int 
{ 
    var i:int = 0; 
    for (var key:Object in d)
    {
        Alert.show(String(key + "\n" + d[key]));
        i++;
    }
    return i; 
} 

这会创建弹出警报,显示所有内容均已正确加载到字典中。

后来,在一个 child 中,我调用了一个尝试使用字典的函数,并且得到了“未定义”的返回。

这是根据键搜索并从数组中返回值的函数:

public function getStudentName(sID:Number):String
{
    return studentsListDict[sID][1];
}

不幸的是,getStudentName 函数每次都返回 undefined。

如果有人能看到我缺少的东西,我将不胜感激。

谢谢, Brds

编辑

它不起作用,因为你不能将数字作为字典中的键。只需在声明期间将它们转换为字符串并查找似乎就可以正常工作。

最佳答案

Here is some documentation on dictionary keys.

看起来您的代码将其设置为字符串,然后将其作为数字访问。我怀疑这是你问题的根源你可以尝试这样的事情:

public function getStudentName(sID:Number):String
{
    return studentsListDict[sID.toString()][1];
}

使用数字作为字典的键实际上是完全可以接受的。字典显然将数字和该数字的字符串值转换为相同的键。这是一个示例:

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

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            public var dict : Dictionary;

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {

                dict = new Dictionary();
                dict["0"] = "hi";
                dict["4"] = "hola";
                dict["17"] = "bye";
                dict["32"] = "adios";
                dict[32] = "adios 2";
                dict[3.2] = "adios 3";
                dict[50] = "Audio ";
                dict["50"] = "Audio 2";

                trace(dict["0"]);
                trace(dict["4"]);
                trace(dict["17"]);
                trace(dict["32"]);
                trace(dict[32]);
                trace(dict[3.2]);
                trace(dict[50]);
                trace(dict["50"]);


            }

        ]]>
    </fx:Script>
</s:Application>

关于apache-flex - 弹性3 : Can anybody see why this dictionary isn't working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6699185/

相关文章:

python - 内存效率 : One large dictionary or a dictionary of smaller dictionaries?

arrays - 为什么不能在 Go 中分配给 map 内的数组?

python - 无法使用 pop 重命名 Python 字典键值 - 错误?

flash - AIR、Flex - 如何检查正则表达式是否有效

css - 在 Flex3 Air 中创建和编辑 CSS

php - 使用 Flex 移动设备登录系统

c++ - 将 std::map 的数据拆分为多个 std::vectors

c++ - 2D游戏 map 移动

apache-flex - Flex 中的某些 [Bindable] 属性有效,有些则无效

css - 是否可以将 bootstrap 用于 adobe flex?