4 Replies Latest reply on Nov 6, 2009 6:08 AM by jmandawg

    List selectedIndex bug??

    jmandawg

      Hi,

       

      I have a mx:list that contains 4 items.  When i remove one item in the list I want the item directly beneath that item selected (It will have the same index as the removed item).  For some reason this doesn't work correctly.  Here is my sample code.  in the example if you remove "b", "c" should be selected. (I know i don't have error checking in for the bottom of the list ect.)

       

      What am i doing wrong??

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="init()">
         
          <mx:Script>
              <![CDATA[
                  import mx.controls.Alert;
                  import mx.collections.ArrayCollection;
                  [Bindable]
                  public var myClassData:ArrayCollection = new ArrayCollection();
                 
                  function init()
                  {
                      myClassData = new ArrayCollection();
                      myClassData.addItem("a");
                      myClassData.addItem("b");
                      myClassData.addItem("c");
                      myClassData.addItem("d");
                  }
                  function removeItem(event:Event)
                  {
                      var oldIndex:int = listMyClasses.selectedIndex;
                      myClassData.removeItemAt(listMyClasses.selectedIndex);
                      if(myClassData.length > 0)
                      {
                         
                          if(myClassData.length -1 >= oldIndex)
                          {
                              //Alert.show("Setting Index to: " + oldIndex);
                              listMyClasses.selectedIndex = oldIndex;
                             
                          }
                         
                          //Alert.show("Currently selected Index: " + listMyClasses.selectedIndex.toString());
                      }
                  }
              ]]>
          </mx:Script>
              <mx:List id="listMyClasses" width="100" height="400" dataProvider="{myClassData}"  />
              <mx:Button label="remove Item" click="{removeItem(event);}" />
              <mx:Button label="Reset List" click="{init();}" />
      </mx:Application>

        • 1. Re: List selectedIndex bug??
          chris.huston.t10 Level 3

          You need to give time for the list to update after the removal. Using the callLater function is one way to do this as shown below:

           

                      private function removeItem(event:Event):void
                      {
                          if (myClassData.length == 0) return;
                          if (listMyClasses.selectedIndex == -1) return;
                          var oldIndex:int = listMyClasses.selectedIndex;
                          myClassData.removeItemAt(listMyClasses.selectedIndex);
                          if(myClassData.length -1 < oldIndex) oldIndex--;
                          if(myClassData.length > 0)
                          {
                              listMyClasses.callLater(set_selected,[oldIndex]);
                             
                          }
                      }
                     
                      private function set_selected(oldIndex:int):void {
                          listMyClasses.selectedIndex = oldIndex;
                      }

           

          Chris

           

          1 person found this helpful
          • 2. Re: List selectedIndex bug??
            jmandawg Level 1

            I've tried the calllater technique, but it seems very inconsistent as it will work sometimes but fail other times.

            Is there anyway to make this consistent?

             

            Is there a way i can fire an event when the List is updated?

            • 3. Re: List selectedIndex bug??
              Subeesh Arakkan Level 4

              Hi,

               

              updateComplete event is dispatched whenever the list is updated.Store the oldIndex in a variable globally. You can use a boolean variable to check if the update is coming from removing the item and then set the selectedIndex from the event handler.

              • 4. Re: List selectedIndex bug??
                jmandawg Level 1

                I guess this is the only way to do it.  It seems like alot of work to accomplish something so simple...