views:

1435

answers:

3

how to select an item in the list as default

default selected item say of index 0

I tried stuff like this --

listid.selectedIndex = somevalueinmyprogram - 1; // 0

but when i debug this i get
_selectedIndex = 0 selectedIndex = -1

and default value is not selected why so? [i have already checked for the obvious that somevaluefrommyprogram is not equal to 0]

Help!

A: 

what does your actual code look like because that is how you set the selected index of a list

Shua
A: 

When are you setting the selectedIndex? After your dataProvider is set I hope.

could you provide your to see your problem?

AndrewB
+1  A: 

I have found that if you set the selectedItems by defining an array of selected items it works better than the selectedIndex.

function setSelectedCategories():void{
   var selectedItems :Array = new Array();      
   for each (var selectedCategory:Category in entry.categories)      {
         for each (var category:Category in categories)         {       
              if (selectedCategory.categoryID == category.categoryID){
                     selectedItems .push(category);               
                     break; 
              }       
         }   
   }   

   categoriesList.selectedItems = selectedItems ;
}

OR using the selectedIndices works if you want to use an array that contains the indexes that you want to be selected.

for ( var i:int=0; i < userIpods.length; i++ ) {

     //j will represent the list item's index value

     for ( var j:int = 0; j < iPodAry.length; j++) {

         if ( userIpods[i] == iPodAry[j].id ) {

         selectedIpodIndices.push( j );
         break;

         } //end if

     } //end for ( var iPodObj:Object in iPodAry) {


} //end for ( var i:int in userIpods ) 


/*mark as selected those index values in the 
selectedIpodIndices array*/

iPodList.selectedIndices = selectedIpodIndices ;
Yack