Using the IViewCursor interface

The ICollectionView interface includes a createCursor() method that returns an IViewCursor object, also called a cursor, that you can use to traverse the items in the view and to access and modify data in the collection. A cursor is a position indicator; it points to a particular item in the collection. You can use IViewCursor methods and properties to perform the following operations:

When you use standard Flex collection classes, ArrayCollection and XMLListCollection, you use the IViewCursor interface directly, you do not reference an object instance, as shown in the following code snippet:

public var myAC:ICollectionView = new ArrayCollection(myArray); 
public var myCursor:IViewCursor;
.
.
myCursor=myAC.createCursor();

Subtopics

Manipulating the view cursor
Getting, adding and removing items
Using bookmarks

Manipulating the view cursor

The IViewCursor interface includes the following methods and properties for moving the cursor:

Getting, adding and removing items

The IViewCursor interface includes the following methods and properties for accessing and changing data in the view:

The following example shows the results of using the insert() and remove() on the current property:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
        initialize="initData();">
    <mx:Script>
        <![CDATA[
            import mx.collections.*;
            public var myArray:Array = [{label:"MA", data:"Massachusetts"}, 
            {label:"MN", data:"Minnesota"}, {label:"MO", data:"Missouri"}];
            [Bindable]
            public var myAC:ArrayCollection; 
            public var myCursor:IViewCursor;

            // Initialize the ArrayCollection when you initialize the
            // application.
            public function initData():void {
                myAC = new ArrayCollection(myArray); 
            }

            // The function to change the collection, and therefore the Array.
            public function testCollection():void {
                // Get an IViewCursor object for accessing the collection data.
                myCursor=myAC.createCursor();
                ta1.text="At start. cursor is at: " + myCursor.current.label;
                var removedItem:String=String(myCursor.remove());
                ta1.text+="\nAfter removing the current item, the cursor is at: " 
                    + myCursor.current.label;
                myCursor.insert({label:"ME", data:"Augusta"});
                ta1.text+="\nAfter adding an item, the cursor is at: " 
                    + myCursor.current.label;
            }
        ]]>
    </mx:Script>

    <mx:ComboBox id="myCB" rowCount="7" dataProvider="{myAC}"/>
    <mx:TextArea id="ta1" height="75" width="350"/> 
    <mx:Button label="run test" click="testCollection();"/>

</mx:Application>

Using bookmarks

You use a bookmark to save a cursor location for later use. You can also use the built-in FIRST and LAST bookmark properties to move the cursor to the first or last item in the view.

To create and use a bookmark:

  1. Move the cursor to a desired location in the view.
  2. Assign the current value of the bookmark property to a variable, as in the following line:
    var myBookmark:CursorBookmark=myIViewCursor.bookmark;
    
  3. Do some operations that might move the cursor.
  4. When you must return to the bookmarked cursor location (or to a specific offset from the bookmarked location), call the IViewCursor seek() method, as in the following line:
    myIViewCursor.seek(myBookmark);
    

The following example counts the number of items in a collection between the selected item in a ComboBox and the end of the collection, and then returns the cursor to the initial location:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        initialize="run();">
    <mx:Script>
        <![CDATA[
            import mx.collections.*;
            private var myCursor:IViewCursor;

            // Initialize variables.
            public function run():void {
                // Initialize the cursor.
                myCursor=myAC.createCursor();
                // The findFirst() method, used in countFromSelection() requires a
                // sorted view.
                var sort:Sort = new Sort();
                sort.fields=[new SortField("label")];
                myAC.sort=sort;
                //You must refresh the view to apply the sort.
                myAC.refresh();
            }

            // Count the items following the current cursor location.
            public function countLast(theCursor:IViewCursor):int {
                var counter:int=0;
                // Set a bookmark at the current cursor location.
                var mark:CursorBookmark=theCursor.bookmark;
                // Move the cursor to the end of the Array.
                // The moveNext method returns false when the cursor is after the
                // last item.
                    while (theCursor.moveNext()) {
                    counter++;
                }
                // Return the cursor to the initial location.
                theCursor.seek(mark);
                return counter;
            }

            // Function triggered by ComboBox change event.
            // Calls the countLast() function to count the number of items to the 
            // end of the collection.
            public function countFromSelection():void {
                myCursor.findFirst(myCB.selectedItem);
                var count:int = countLast(myCursor);
                ta1.text += myCursor.current.label + " is " + count +
                                " from the last item.\n";
            }
        ]]>
    </mx:Script>

    <!-- The data provider, an ArrayCollection with an array of objects. --> 
    <mx:ArrayCollection id="myAC">
        <mx:Object label="MA" data="Boston"/>
        <mx:Object label="ME" data="Augusta"/>
        <mx:Object label="MI" data="Lansing"/>
        <mx:Object label="MN" data="Saint Paul"/>
        <mx:Object label="MO" data="Jefferson City"/>
        <mx:Object label="MS" data="Jackson"/>
        <mx:Object label="MT" data="Helena"/>
    </mx:ArrayCollection>

    <mx:ComboBox id="myCB" rowCount="7" dataProvider="{myAC}" change="countFromSelection();"/>
    <mx:TextArea id="ta1" height="200" width="175"/>
</mx:Application>

Flex 2

 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/2/docs/00000505.html