Displaying the row number or index in a spark List

The ItemRenderer class in Flex 4 has an itemIndex property that was added after beta2. You can use this property within your item renderer to display the row number or index of an item in a spark List.

The following example demonstrates a List with a custom item renderer that uses this itemIndex property. If you look at the commented out code in the item renderer you will also see a way of getting this information using the getItemIndex() method of the dataProvider.

View Source

Note: This sample requires Flex SDK 4.0.0.12093 or higher. You can get the latest SDK builds from opensource.adobe.com.

8 thoughts on “Displaying the row number or index in a spark List”

  1. Has Flex 4 solved the issue that sometimes the set data function will get called before the component is instantiated? If not, it seems like using the setter to directly reference the labels could result in NPE errors.

  2. @Amy – The data setter gets called after the renderer is added to the display list and all of the renderer’s children (such as the Label) are created. This should be the same behavior as in halo. Do you have an example of a case where this failed for you in Flex 3?

  3. hello, I have may items to be displayed in the list. I can see 6 of them in the list and I need to scroll down to see others. In this case 7th item get index 0 and 8th item get index 1 and goes on… it is related to the displaying items in a list I think, how can I prevent this.

  4. I just found the solution, I changed useVirtualLayout property to false, not it works like here

  5. @baki – Make sure that your renderer doesn’t have any logic on the creationComplete event when in a virtual layout.

  6. after making deletions how can I save a custom list as xml along with row numbers

  7. Hello, I have one issue in this list. on selection change drop down list i am changing my list item some time in place of 0(zero) index my list showing 20 then like. 20 1 2 3 4 …
    how can i fix this problem.

Comments are closed.