retrieve the label value from a TileList (Flash CS5 AS3)

1.4k views Asked by At

I have a TileList, that i loaded with a PHP script that gives back an dinamically generated xml so i can load the TileList with the images and labels it needs.

Now, i want to click on an item of the TileList an retrieve its Label so i can send it to another PHP script that uses the string of the Label to do a search query so i can load latitud and longitude to a map to where that item is located.

i have been trying to trace it, but its a no go.

var path:String = "http://localhost/entretenimiento.php";
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, onLoadComplete);
xmlLoader.load(new URLRequest(path));       

function onLoadComplete(e:Event):void {
    System.disposeXML(xmlData);
    var xmlData:XML = new XML(e.target.data);
    //trace(xmlData);

    for (var i:int=0; i<xmlData.e_nombre.length(); i++)
    {
        myTileList.addItem({label:xmlData.e_nombre[i], source:xmlData.e_imagen[i]});
        //trace(xmlData.e_nombre[i]);
    }

    var display;
    myTileList.addEventListener(Event.CHANGE, listListener);
    myTileList.selectedIndex = 0;

    function listListener(event:Event):void {
    display.source = myTileList.selectedItem.label;
    trace(display.source);
    trace("working");

    }

}

I need help, i dont know how to retrieve the label from item clicked on the TileList.

Im using Flash CS5.

2

There are 2 answers

0
Saul On BEST ANSWER

Ok, well...

this was simple, after a nap i just sat down and tried this and it gave me what i wanted.

var path:String = "http://localhost/entretenimiento.php";
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, onLoadComplete);
xmlLoader.load(new URLRequest(path));       

function onLoadComplete(e:Event):void {
  System.disposeXML(xmlData);
  var xmlData:XML = new XML(e.target.data);
  //trace(xmlData);

  for (var i:int=0; i<xmlData.e_nombre.length(); i++)
  {
      myTileList.addItem({label:xmlData.e_nombre[i], source:xmlData.e_imagen[i]});
      //trace(xmlData.e_nombre[i]);
  }

  myTileList.addEventListener(Event.CHANGE, listListener);
  myTileList.selectedIndex = 0;

  function listListener(event:Event):void {

  trace(myTileList.selectedItem.label);

  }

}
3
Anthony Maitz On
// this should live somewhere in your code:
var path:String = "http://localhost/entretenimiento.php";
var xmlLoader:URLLoader = new URLLoader();
    xmlLoader.addEventListener(Event.COMPLETE, onComplete);
    xmlLoader.load( new URLRequest( path ));

private function onComplete(e:Event):void
{
    // XML works just fine as a data provider, you don't need the loop.
    myTileList.dataProvider = new DataProvider(new XML(e.target.data));
    // You can assign which properties of the xml the list uses.
    myTileList.sourceField = "e_imagen";
    myTileList.labelField = "e_nombre"; 
    // I'm assuming that the goal is to have the user select an item, 
    //     so we'll register to listen for a click.
    myTileList.addEventListener( MouseEvent.CLICK, listListener );
}

private function listListener(e:Event):void
{
    /* 
    Because we're listening for a click, and the listener is registered to the list
    make sure that what we've clicked is an item (a CellRenderer) and not the
    background or something.
    */

    if ( getDefinitionByName(getQualifiedClassName(e.target)) == CellRenderer )
    {
        // You can call your other web service from here.
        // The "target" is a CellRenderer, and its data is an XML node.
        trace(e.target.data.label);
    }
}