Flash AS3 TileList DataProvider getIndex

394 Views Asked by At

I am using the Flash code provided on the web. Im modifying it as to differentiate the colour of odd numbered row Background and the even one. But I cant get access to the data.index. How do I get access to the index?

import fl.controls.TileList;
import fl.core.UIComponent;
import fl.data.DataProvider;
import fl.events.ListEvent;
import flash.display.Sprite;
import flash.events.Event;

public class CellRendererExample extends Sprite
{
    public function CellRendererExample() {
        var index:int;
        var dp:DataProvider = new DataProvider();
        var totalEntries:uint = 42;
        var i:uint;
        for(i=0; i<totalEntries; i++) {
            dp.addItem( { label:"Item "+i ,index:i} );            
        }

        var myTileList = new TileList();
        myTileList.dataProvider = dp;
        myTileList.allowMultipleSelection = true;
        myTileList.columnWidth = 125;
        myTileList.rowHeight = 30;
        myTileList.columnCount = 1;
        myTileList.rowCount = 6;
        myTileList.move(10,10);
        myTileList.direction = "VERTICAL";


        for(i=0; i<totalEntries; i++) {
        if (dp.label[i].index%2==0){
        myTileList.setStyle('cellRenderer', MyRenderer);    }
        else{
            myTileList.setStyle('cellRenderer', MyAnotherRenderer); 
            }
        }


        addChild(myTileList);
    }
1

There are 1 best solutions below

7
Andrey Popov On

This line is completely wrong:

if (dp.label[i].index%2==0){

DataProvider just contains your items, similar to Array. label[i] won't return anything, and index is not a property of that object (label and index are properties of the object that you put into the DataProvider).

First, your i variable is actually the index. You can check if i % 2 == 0. If you want any specific information from the DataProvider (you put different indexes in the elements), you could use dp.getItemAt(i) -> this will give you each item, and you can check it's properties: dp.getItemAt(i).index % 2 == 0.