0

my layout in .mxml file is like this:-

<?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" width="600" height="800" >
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
        <mx:HBox width="100%" horizontalAlign="right" top="20">
            <mx:ComboBox id="comboBox" fontSize="12" width="10%" dataProvider="{middleStack}"  name="versioncombo"/>
        </mx:HBox>
        <mx:VBox top="50" left="10" >
            <mx:Label text="Builds" fontSize="18">
            </mx:Label>
            </mx:VBox>
        <mx:VBox top="50" horizontalAlign="center" left="200">
            <mx:Label text="Date" fontSize="18">
            </mx:Label>
        </mx:VBox>
        <mx:VBox top="50" horizontalAlign="center" right="10">
            <mx:Label text="Release" fontSize="18">
            </mx:Label>
        </mx:VBox>
        <mx:VBox height="100%" width="100%" styleName="centerLightGreyBg" verticalGap="0">

            <mx:ViewStack id="middleStack" width="100%" height="100%" backgroundAlpha="0" change="changev()">
            </mx:ViewStack>

        </mx:VBox>
        <fx:Style>

Now I want to add values to the combo box with id = combobox and name = versioncombo by reading values from an XML File. My xml is like this.

<Install>
  <version>
    <number>5.0</number>
    <build>907681</build>
    <path></path>

  </version>
  <version>
    <number>6.2</number>
    <build>1043305</build>
    <path></path>

  </version>
</Install>

I want to populate the combo box with version number i.e 5.0,6.2.

I tried to follow this link but was not able to do it http://www.parorrey.com/blog/flash-development/as3-adding-dropdown-combobox-flash-component-using-actionscript-with-xml-data/

AS3 Procedural. Randomise order of Items in Combobox From XMl

I am very new to AIR.

I tried it like this:-

import fl.data.DataProvider; 

            var country:String='';
            var countriesList:Array = [];

            //URLLoader class helps you to load data from an external source such as a URL

            var xmlLoader:URLLoader ; 
            var xmlData:XML = new XML(); 

            public function Pathfinding() {
                // constructor code
                xmlLoader = new URLLoader();
                xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
                xmlLoader.load(new URLRequest("grid.xml"));
            }

            function LoadXML(e:Event):void { 
                xmlData = new XML(e.target.data); 
                xmlData.ignoreWhite = true;

                ParseCountries(xmlData); 
                //trace(xmlData);
            } 

            function ParseCountries(countriesInput:XML):void { 

                var countriesListing:XMLList = countriesInput.country.attributes(); 
                var i:int;  

                for each (var countryName:XML in countriesListing) { 

                    //populate the countriesList array to be used with ComboBox
                    countriesList.push( {label:countryName, data:countryName} );
                    trace(countryName);

                }

                versioncombo.prompt = "Select version:"; 
                versioncombo.dataProvider = new DataProvider(countriesList); 
                versioncombo.addEventListener(Event.CHANGE, dropDownHandler);
                versioncombo.dropdown.rowHeight  = 30;
                versioncombo.width = 150;

            } 


            function dropDownHandler(event:Event):void { 

                country = ComboBox(event.target).selectedItem.data;     
                trace("selected country: "+ country);
                //do something here whatever you want 
            }   
comboBox.prompt = "Select Country:"; 
            comboBox.dataProvider = new DataProvider(countriesList); 
            comboBox.addEventListener(Event.CHANGE, dropDownHandler);
            comboBox.dropdown.rowHeight  = 30;
            comboBox.width = 150;

but was getting error "-1180: Call to a possibly undefined method DataProvider."

                comboBox.dataProvider = new DataProvider(countriesList); 

not working for me.

Community
  • 1
  • 1
user3649361
  • 944
  • 4
  • 20
  • 40

2 Answers2

1

You are confusing the name property with the id property of the ComboBox component you have defined. It is the id you need to use to refer to the ComboBox instance.

from Working with Components:

You should declare an instance variable for each dynamically created component and store a reference to the newly created component in it, just as the MXML compiler does when you set an id property for a component instance tag. You can then access your dynamically created components in the same way as those declaratively created in MXML.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
spring
  • 18,009
  • 15
  • 80
  • 160
1

You are need just read about correct work with dataprovider. You didn't need a new instance creation, like "new DataProvider". Please, use ArrayCollection or any of IList

strangedk
  • 19
  • 5