The following example shows how you can loop through items in a Spark List control in Flex 4 by setting the Boolean arrowKeysWrapFocus property.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/03/looping-through-items-in-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_arrowKeysWrapFocus_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark">
    <s:controlBarContent>
        <s:CheckBox id="ch"
                label="arrowKeysWrapFocus:"
                selected="true" />
    </s:controlBarContent>
 
    <s:List id="lst"
            arrowKeysWrapFocus="{ch.selected}"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:VerticalLayout gap="0"
                    horizontalAlign="contentJustify"
                    requestedRowCount="5" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:Array>
                    <fx:Object label="Application" />
                    <fx:Object label="BorderContainer" />
                    <fx:Object label="Button" />
                    <fx:Object label="ButtonBar" />
                    <fx:Object label="ButtonBarButton" />
                    <fx:Object label="CheckBox" />
                    <fx:Object label="ComboBox" />
                    <fx:Object label="DataGroup" />
                    <fx:Object label="DataRenderer" />
                    <fx:Object label="DropDownList" />
                </fx:Array>
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

View source is enabled in the following example.

Or you can set the Boolean arrowKeysWrapFocus property using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/03/looping-through-items-in-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_arrowKeysWrapFocus_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark">
    <s:controlBarContent>
        <s:CheckBox id="ch"
                label="arrowKeysWrapFocus:"
                change="lst.arrowKeysWrapFocus = ch.selected;" />
    </s:controlBarContent>
 
    <s:List id="lst"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:VerticalLayout gap="0"
                    horizontalAlign="contentJustify"
                    requestedRowCount="5" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:Array>
                    <fx:Object label="Application" />
                    <fx:Object label="BorderContainer" />
                    <fx:Object label="Button" />
                    <fx:Object label="ButtonBar" />
                    <fx:Object label="ButtonBarButton" />
                    <fx:Object label="CheckBox" />
                    <fx:Object label="ComboBox" />
                    <fx:Object label="DataGroup" />
                    <fx:Object label="DataRenderer" />
                    <fx:Object label="DropDownList" />
                </fx:Array>
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

5 Responses to Looping through items in a Spark List control in Flex 4

  1. Michael Novotny says:

    Peter : :

    I had a couple of questions on this post…

    1. I copy and pasted this example into a new Flex applcation and it ran perfectly. But then I changed “requestedRowCount” to 3 instead of 5, but it still showed 5. Any ideas why that would be?

    2. Then, for extra kicks, I tried to push this a bit further… One of the things that’s really bothered me and my company is how much extra work it takes to set the “requestedRowCount” on a DropDownList as written about in your blog post here last April (http://blog.flexexamples.com/2009/04/05/setting-the-row-count-on-a-dropdownlist-control-in-flex-gumbo/). So, for fun, I replaced s:List with s:DropDownList in this example to see if I could get away with significantly less code to pull off a “requestedRowCount”. I ran it and it sorta worked… The DropDownList opened and I saw only 3 items listed, but then it immediately hit a RTE (Error #1009: Cannot access a property or method of a null object reference.) in DropDownListBase on setting the “spDelta” on line 662 (the dataGroup was null). I’ve tried several things to “trick it” like giving it a DataGroup, but that results in other RTE’s due to there not being a “typicalItem” or invalid “dataGroupProperties” (I can give you more details on those RTE’s as well if you’d like). At any rate… It seems to me that if “requestedRowCount” work with list as it does in this example without an RTE, that it should in DropDownList too. Can you please explain why it’s necessary for two custom classes just to get “requestedRowCount” on a DropDownList?

    Thanks for your time in reading this and for doing these examples. They are invaluable! And, if it makes any difference in my question, the Flex 4 code we’re using at our company is around the 12073 revision mark from the Adobe repository. Thanks again and I look forward to your reply!

    : : Michael

    • Peter deHaan says:

      @Michael,

      The requestedRowCount works fine for me in 4.0.0 (build 13895). I’m pretty sure we fixed that issue where the minHeight property on the Spark List skin was getting set to 112px (5 items+border if using default item renderer). You can get around the issue by creating a custom Spark List skin without the minHeight property set, or even better would be to download a newer Nightly build of the Flex 4.0 SDK from http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

      As for question #2, I’d download the latest nightly build of Flex 4.0 SDK and try again, if you’re still seeing an RTE, I’d file a bug right away at http://bugs.adobe.com/flex/ and somebody at Adobe will take a look.

      Thanks,
      Peter

    • Peter deHaan says:

      Actually, I looked at that mess of a post on Spark DropDownList and row counts, and I found a way easier way in the latest SDK (or maybe it always worked and I missed the obvious before). You can just override the layout property on the DropDownList and set the requested row count on the VerticalLayout object directly.

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application name="Spark_DropDownList_layout_requestedRowCount_test"
              xmlns:fx="http://ns.adobe.com/mxml/2009"
              xmlns:s="library://ns.adobe.com/flex/spark">
          <s:controlBarContent>
              <s:CheckBox id="ch"
                      label="arrowKeysWrapFocus:"
                      selected="true" />
          </s:controlBarContent>
       
          <s:DropDownList id="lst"
                  requireSelection="true"
                  arrowKeysWrapFocus="{ch.selected}"
                  horizontalCenter="0" verticalCenter="0">
              <s:layout>
                  <s:VerticalLayout gap="0"
                          horizontalAlign="contentJustify"
                          requestedRowCount="2" />
              </s:layout>
              <s:dataProvider>
                  <s:ArrayList>
                      <fx:Array>
                          <fx:Object label="Application" />
                          <fx:Object label="BorderContainer" />
                          <fx:Object label="Button" />
                          <fx:Object label="ButtonBar" />
                          <fx:Object label="ButtonBarButton" />
                          <fx:Object label="CheckBox" />
                          <fx:Object label="ComboBox" />
                          <fx:Object label="DataGroup" />
                          <fx:Object label="DataRenderer" />
                          <fx:Object label="DropDownList" />
                      </fx:Array>
                  </s:ArrayList>
              </s:dataProvider>
          </s:DropDownList>
       
      </s:Application>

      Again, I tested in 4.0.0 (build 13895) and it worked like I expected, but please download the latest nightly build and make sure you’re still not seeing any runtime errors (and file a bug if you do).

      Thanks,
      Peter

      • Michael Novotny says:

        Peter : :

        Thanks for the prompt reply, for taking the time to revisit and test some things out, and for reposting an example. Copy and pasting the code above in your second reply into our environment still produces an RTE, but as I mentioned in my first post, we are on / around revision 12073. I will acquire the latest nightly build and report back. Thanks again for your help!

        : : Michael

      • Michael Novotny says:

        Peter : :

        Sorry for the delay in getting back to you. We have been pretty busy prepping for the 1.0 release of our product.

        We upgraded our SDK last week to revision 14184. I retested this and it works perfectly (with the knowledge of the > 6 limitation because of the DropDownListSkin).

        Thanks for all your help on this!

        : : Michael