jimmy
jimmy

Reputation: 5

Auto select the first index in a sparks List control in flex4

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 can I achieve this?

Upvotes: 1

Views: 2228

Answers (4)

Ariel Scarpinelli
Ariel Scarpinelli

Reputation: 310

You should set the requireSelection property to true on the list control

<s:List id="myList" dataProvider="{myDataProvider}" requireSelection="true"/>

Upvotes: 1

bug-a-lot
bug-a-lot

Reputation: 2454

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.

Upvotes: 0

Robusto
Robusto

Reputation: 31883

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();
  }
}

Upvotes: 0

Teerasej
Teerasej

Reputation: 1536

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.

Upvotes: 0

Related Questions