Tuesday, December 2, 2008

mx.controls.List and empty dataProviders

If you bind a List's dataProvider to an ArrayCollection that happens to be empty at the time of binding, the List will ignore it and use its own ArrayCollection as its dataProvider. Later, when you populate your ArrayCollection, the List won't show the changes since it isn't using it.

Yes, really.

I have submitted this to Adobe as http://bugs.adobe.com/jira/browse/SDK-18288.

There are two workarounds:

1. Don't bind to an empty ArrayCollection. Pre-initialize the ArrayCollection to something non-empty. For example:
[Bindable]
public var results : ArrayCollection = new ArrayCollection([ "" ]);

2. Don't bind until after you populate the ArrayCollection with real data:
public function populateMyCollection() : void
{
results.addItem("Hello");
results.addItem("World");

myList.dataProvider = results;
}

No comments: