1 Reply Latest reply on Apr 21, 2011 4:52 PM by brock_b

    Can I dynamically set selectedItem for a list?

    brock_b Level 1

      I know I can set the selected index.  I've got a list where once a user selects a name from the list it goes over to another panel and fills out all of the information for them and they can then select to use that name/individual chosen.  All works well there.  However I take that userid that was stored when they selected the user and if they need to go back and edit the user I'm trying to make it to where I can pass that userID... find the location in the dataprovider and then continue on to where it automatically selects the user in the list for them and pulls up the user info without them having to physically select it like they did originally.


      I can find the location and use something like :  csi.selectedIndex = 3;  if that is the location in the dataProvider.  However my code pulls data from that dataprovider based on selectedItem (it pulls names, phone numbers, addresses, etc...) and it works fine when manually selected, but if I select the user via actionscript by setting csi.selectedIndex = 3... the selectedItem is null;


      So I guess my simple question to a maybe not so simple solution is... what else do I need to do besides set the selectedIndex for me to be able to access the selectedItem of a list?


      Thanks for any assistance,

        • 1. Re: Can I dynamically set selectedItem for a list?
          brock_b Level 1

          Nevermind.  I did a really simple example like below and it worked as expected.


          <?xml version="1.0" encoding="utf-8"?>
          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                            xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
                    <!-- Place non-visual elements (e.g., services, value objects) here -->
                    <mx:ArrayCollection id="people">
                              <fx:Object label="Bob Barker" firstName="Bob" lastName="Barker" phone="123-456-7890" />
                              <fx:Object label="Richard Dawson" firstName="Richard" lastName="Dawson" phone="234-567-8901" />
                              <fx:Object label="Alex Trebek" firstName="Alex" lastName="Trebek" phone="345-678-9012" />
                              <fx:Object label="Gene Rayburn" firstName="Gene" lastName="Rayburn" phone="456-789-0123" />
                              <fx:Object label="Chuck Woolery" firstName="Chuck" lastName="Woolery" phone="567-890-1234" />
                              <fx:Object label="Monty Hall" firstName="Monty" lastName="Hall" phone="678-901-2345" />
                              <fx:Object label="Pat Sajak" firstName="Pat" lastName="Sajak" phone="789-012-3456" />
                              <fx:Object label="Dick Clark" firstName="Dick" lastName="Clark" phone="890-123-4567" />
                              <fx:Object label="Peter Marshall" firstName="Peter" lastName="Marshall" phone="901-234-5678" />
                              <fx:Object label="Bob Eubanks" firstName="Bob" lastName="Eubanks" phone="012-345-6789" />
                         import mx.controls.Alert;
                         import mx.utils.ObjectUtil;
                         private function showData():void
                         private function setAndShow():void
                              cList.selectedIndex = 2;
                    <s:List id="cList" dataProvider="{people}" change="showData()" />
                    <s:Button label="set and show Alex Trebek" click="setAndShow()" />


          So I went back to my more complex example and after setting the selectedIndex I set a timeout on the call to the function that pulls in the selectedItem info and it worked.  I guess I need to setup an event listener to make sure the selectedIndex change has completed before calling my other function that utilizes the selectedItem information.  I figured I could make that call immediately after setting the selectedIndex but in this case it doesn't look like I can.  =(