PDA

View Full Version : How to create checkboxes from ArrayCollection


kingd
09-14-2010, 03:15 PM
I have an ArrayCollection from which I want to create checkboxes that are divided into sections with headers, like below:

FICTION
[] The Sleeper Awakes -- H.G. Wells [] The Time Machine -- H.G. Wells
[] The Invisible Man -- H.G. Wells [] War of the Worlds -- H.G. Wells

I've tried the following, but was unable to addElements to the list. Anyone know what I can do to addElements and create the list?


...
var books:ArrayCollection = new ArrayCollection([{category: "fiction", title: "The Time Machine", author: "H.G. Wells"},...]);
var list:List = new List();
list.layout = new TileLayout();

for (var i:int = 0; i < books.length; i++)
{
var checkBox:CheckBox = new CheckBox();
checkBox.id = "book";
checkBox.label = String(books.getItemAt(i).title);
list.addElement(checkBox);
// Error -- 1061: Call to a possibly undefined method addElement through a reference with static type spark.components:List.
}
booksPanel.addElement(list);

drkstr
09-15-2010, 04:38 PM
A List component is data driven. You do not add or remove elements to it, you simply assign a dataProvider and the component will add an element for each data item. You assign an itemRenderer to display those data items in a manor of your choosing.

More info here:

http://help.adobe.com/en_US/flex/using/WSc2368ca491e3ff923c946c5112135c8ee9e-7fff.html

kingd
09-17-2010, 06:09 PM
A List component is data driven. You do not add or remove elements to it, you simply assign a dataProvider and the component will add an element for each data item...

Thanks for the explanation; I wasn't aware of those limitations. By changing to a spark group container, I was able to (mostly) achieve what I wanted.

drkstr
09-17-2010, 09:14 PM
If by limitation you mean awesome feature that makes your life so much easier, then sure. :)