Displaying specific items from an ArrayCollection in Flex

by Peter deHaan on May 10, 2008

in ArrayCollection

The following examples show how you can display specific items in a Flex ArrayCollection using the getItemAt() method and the array access operator ([]).

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/05/10/displaying-specific-items-from-an-arraycollection-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:ArrayCollection id="arrColl">
        <mx:source>
            <mx:Array>
                <mx:Object label="Student A" score="85" />
                <mx:Object label="Student B" score="48" />
                <mx:Object label="Student C" score="71" />
                <mx:Object label="Student D" score="88" />
                <mx:Object label="Student E" score="24" />
                <mx:Object label="Student F" score="64" />
                <mx:Object label="Student G" score="76" />
                <mx:Object label="Student H" score="76" />
                <mx:Object label="Student I" score="93" />
                <mx:Object label="Student J" score="88" />
                <mx:Object label="Student K" score="48" />
                <mx:Object label="Student L" score="76" />
            </mx:Array>
        </mx:source>
    </mx:ArrayCollection>

    <mx:ApplicationControlBar dock="true">
        <mx:HSlider id="slider"
                minimum="0"
                maximum="{arrColl.length-1}"
                liveDragging="true"
                snapInterval="1"
                tickInterval="1"
                dataTipPlacement="right" />
    </mx:ApplicationControlBar>

    <mx:Label text="{arrColl.getItemAt(slider.value).label}" />

</mx:Application>

View source is enabled in the following example.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/05/10/displaying-specific-items-from-an-arraycollection-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:ArrayCollection id="arrColl">
        <mx:source>
            <mx:Array>
                <mx:Object label="Student A" score="85" />
                <mx:Object label="Student B" score="48" />
                <mx:Object label="Student C" score="71" />
                <mx:Object label="Student D" score="88" />
                <mx:Object label="Student E" score="24" />
                <mx:Object label="Student F" score="64" />
                <mx:Object label="Student G" score="76" />
                <mx:Object label="Student H" score="76" />
                <mx:Object label="Student I" score="93" />
                <mx:Object label="Student J" score="88" />
                <mx:Object label="Student K" score="48" />
                <mx:Object label="Student L" score="76" />
            </mx:Array>
        </mx:source>
    </mx:ArrayCollection>

    <mx:ApplicationControlBar dock="true">
        <mx:HSlider id="slider"
                minimum="0"
                maximum="{arrColl.length-1}"
                liveDragging="true"
                snapInterval="1"
                tickInterval="1"
                dataTipPlacement="right"
                change="lbl.text = arrColl[event.value].label;" />
    </mx:ApplicationControlBar>

    <mx:Label id="lbl" />

</mx:Application>

{ 5 comments… read them below or add one }

1 Jon Henley September 2, 2008 at 9:05 am

Hey

How would I use a string variable instead of the ‘label’ … if that is dynamic?

is it possible with getItemAt?

Reply

2 peterd September 3, 2008 at 7:26 am

Jon Henley,

You can try using the Array access syntax to specify the field name. Something like the following:

<mx:String id="str">score</mx:String>
<mx:Label text="{arrColl.getItemAt(slider.value)[str]}" />

Flex Builder may give you a warning saying “Data binding will not be able to detect changes when using square bracket operator. For Array, please use ArrayCollection.getItemAt() instead.”, but you can probably ignore that.

Peter

Reply

3 Bobby February 12, 2009 at 3:37 pm

Ok. I am trying to send a string to a wsdl from an arrayCollection with all values. any thoughts?

Reply

4 larry April 25, 2009 at 8:51 pm

hi!

How about to random the value of array collection by clicking a button.

thank you!

Reply

5 Antonio Ortiz January 8, 2010 at 9:36 am

How you can modify on your example a specific item, for example for student F change the value of the score 64 by 30 on action scriptt?… In advance thanks and grettings from mexico :D

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: