tags:

views:

234

answers:

3

hi

i have a spark List control. it has a dataProvider that shows reply from twitter search.

i have created a function for change handler like this:

protected function list_changeHandler(event:IndexChangeEvent):void { ta.text = coverflow.selectedItem.title; }

so, whenever i select each of the items in the List i will see the message(ta.text) but now, instead of me manually clicking the first time, i want it to automatically click/select the first item and see the first message(ta.text)

how do i do it? ...

newbie to flex ... :p

A: 

Actually, in thinking about this, you probably need to subclass the list and override the dataProvider setter.

override public function set dataProvider(data:*) : void {
  super._dataProvider = data;
  // This will be an ArrayCollection or XMLListCollection, so will have a length
  if (data && data.length > 0) {
    this.selectedIndex = 0;
    invalidateDisplayList();
  }
}
Robusto
No need for subclassing unless he wants the same behavior in more than one place.
bug-a-lot
A: 

Hi, james

How about to try this solution? :)

Your list control also has event name creationComplete (similar to change event). Try to select your first item with this:

protected function list1_creationCompleteHandler(event:FlexEvent):void
{
    if(event.target.dataProvider != null )
    {
        (event.target as List).selectedIndex = 0;
    }
}

You may not need to convert event.target to List. but it may help you access code completion while you are coding.

Teerasej
This will fail if the dataProvider is null at creationComplete.
Robusto
Thank, Robusto. I improved my answer follow your mention.
Teerasej
I'd also recommend writing your assignment as `List(event.target).selectedIndex = 0;` That way your intellisense will work and the syntax checker won't put up a warning.
Robusto
thanks so much .........
james
A: 

Set the dataProvider of the list, set the selected item, and then either call your handler function directly with a null parameter, or make the list dispatch an indexChanged event so that your handler function gets executed.

bug-a-lot