Flex 3: Can anybody see why this dictionary isn't working? -
Flex 3: Can anybody see why this dictionary isn't working? -
so, in main mxml, have variable defined such:
[bindable] public var studentslistdict:dictionary = new dictionary; i have next imported:
import flash.utils.dictionary; i have httpservice imports xml file:
<mx:httpservice id="studentshttp" url="students.xml" resultformat="e4x" makeobjectsbindable="true" result="createstudentscollection(event)" /> the createstudentscollection function follows:
private function createstudentscollection(e:resultevent):void { var xmllist:xmllist = xml(e.result).student; var dupstring:string = "|"; var temparray:array = new array; studentslistdict = new dictionary; (var i:int = 0; < 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); } } } then, ensure items correctly set dictionary, have next function:
public static function getlen(d:dictionary):int { var i:int = 0; (var key:object in d) { alert.show(string(key + "\n" + d[key])); i++; } homecoming i; } this creates pop alerts show loaded correctly dictionary.
later on, in child, phone call function tries utilize dictionary, , homecoming of "undefined".
here's function searches based on key, , returns value array within:
public function getstudentname(sid:number):string { homecoming studentslistdict[sid][1]; } unfortunately, getstudentname function returns undefined every time.
if can see i'm missing, it'd appreciated.
thanks, brds
edit
it wasn't working b/c can't have numbers keys in dictionary. casting them string during declaration , seems work fine.
here documentation on dictionary keys..
it looks you're code setting string , accessing number. suspect root of problem can seek this:
public function getstudentname(sid:number):string { homecoming studentslistdict[sid.tostring()][1]; } it acceptable utilize numbers keys dictionary. dictionary apparently turns number , string value of number same key. here sample:
<?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> flex dictionary
Comments
Post a Comment