2 Replies Latest reply on May 26, 2010 7:51 PM by UbuntuPenguin

    XML-based dataprovider of Combobox / Dropdownlist (spark)

    Yogi_K

      Hey guys,

      I am struggling a bit with a problem...

       

      I have an XML file which I use as a dataprovider for a combobox / dropdownlist. If I do that just in Flex with MXML (with a HTTP Service call and using its last result) it is working fine. However I need to replace that XML file dynamically.

       

      But if I assign a a section of a dynamically loaded XML file (loading is successful (e.g. xmlFile.Sensors) as dataprovider I get that message:

       

      Type Coercion failed: cannot convert XMLList@18d8b629 to mx.collections.IList.

       

      Well, I came around by that:

       

      var collection:XMLListCollection = new XMLListCollection();
      for each (var t:XML in sensorXml.Sensors)
      {
           collection.addItem(t);
      }

       

      ...and using the collection as a dataprovider. But seems a bit unuseful...isn't there a faster / more direct approach to populate the dropdownlist with its contents? Especially since e.g. the dataGrid offers to use XML files directly as dataproviders...

        • 1. Re: XML-based dataprovider of Combobox / Dropdownlist (spark)
          UbuntuPenguin Level 4

          Try ,

          var collection:XMLListCollection = new XMLListCollection();
          collection.source = sensorXml.Sensors;

           

          Here is a link that goes into more depth ,

          http://thanksmister.com/index.php/archive/binding-xml-to-combobox/

          1 person found this helpful
          • 2. Re: XML-based dataprovider of Combobox / Dropdownlist (spark)
            David_F57 Level 5

            Hi,

             

            The following is a basic app that will dynamically load your xml result into your comboboxes dataprovider.

             

            hope it helps

             

            David.

             

             

             

            <?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/halo" minWidth="955" minHeight="600" creationComplete="app_creationCompleteHandler(event)"">

            <fx:Script>

            <![CDATA[

            import mx.events.FlexEvent;

            import mx.rpc.events.FaultEvent;

            import mx.rpc.events.ResultEvent;

             

            protected function myXML_resultHandler(event:ResultEvent):void

            {

            xmlMainMenu.source = event.result.children();

            }

             

            protected function myXML_faultHandler(event:FaultEvent):void

            {

            //Alert.show("Sorry Couldn't load XML","Error Message");

            }

             

            protected function app_creationCompleteHandler(event:FlexEvent):void

            {

            myXML.send();

            }

             

            ]]>

            </fx:Script>

             

            <fx:Declarations>

            <s:HTTPService id="myXML" url="data.xml" resultFormat="e4x"

               result="myXML_resultHandler(event)" fault="myXML_faultHandler(event)"/>

            </fx:Declarations>

            <s:ComboBox x="57" y="142" width="150" labelField="@label">

            <s:dataProvider>

            <s:XMLListCollection id="xmlMainMenu" />

            </s:dataProvider>

            </s:ComboBox>

            </s:Application>

             

            ======

            the xml

            ======

             

            <?xml version="1.0" encoding="UTF-8" standalone="no" ?>

            <menu>

            <item label="Group One">

            <img url="images/img02.jpg"/>

            </item>

            <item label="Group Two">

            <img url="images/img05.jpg"/>

            <img url="images/img06.jpg"/>

            <img url="images/img07.jpg"/>

            </item>

            <item label="Group Three">

            <img url="images/img08.jpg"/>

            <img url="images/img09.jpg"/>

            <img url="images/img10.jpg"/>

            <img url="images/img04.jpg"/>

            </item>

            <item label="Group Four">

            <img url="images/img04.jpg"/>

            </item>

            <item label="Group Five">

            <img url="images/img09.jpg"/>

            <img url="images/img06.jpg"/>

            </item>

            </menu>