dcsimg
A Flash Developer Resource Site

Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 47

Thread: XML to AS3 to List component in flash

  1. #21
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    No, create a class.
    Code:
    package {
      public class Report{
    
        public var label:String;
        public var data:String;
    
        public function Report(name:String, description:String){
           this.label = name;
           this.data = description;
        }
      }
    }
    Save that in a file called Report.as in the same directory as your FLA.

  2. #22
    Member
    Join Date
    May 2011
    Posts
    39
    okay yep i have done that..

    now the only error i get is: 'access of undefined property reports for the line'

    reports.push(report);

  3. #23
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Allow me to quote myself:
    You also did not declare and instantiate your reports array.
    So somewhere at the top of your script, you need something like:
    Code:
    var reports:Array = [];
    Or you could get rid of that line entirely and rely on the List to keep your report objects.

  4. #24
    Member
    Join Date
    May 2011
    Posts
    39
    hmm sorry for driving you mad it probably is really frustrating helping a newb but i think you are a really good person for doing this where others wouldnt bother!.

    Well no errors now but when the SWF file loads i have empty list box and an empty text box. Dont i need to code the textbox to show the desc when a report is selected

  5. #25
    Member
    Join Date
    May 2011
    Posts
    39
    So this is the full code we have so far


    import fl.controls.List;
    import flash.text.TextField;
    var reports:Array = [];

    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML = new XML();
    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
    xmlLoader.load(new URLRequest("report2.xml"));

    function LoadXML(e:Event):void
    {
    var reportNodes:XMLList = xmlData.reportdetails;
    for (var i:int = 0; i < reportNodes.length(); i++){
    var report:XML = reportNodes[i];
    processReport(report);
    }
    }

    function processReport(r:XML):void{
    var name:String = r.@name.toString();
    var desc:String = r.desc.toString();
    var report:Report = new Report(name, desc);
    rname.addItem(report);
    reports.push(report);
    }

  6. #26
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Please use [code] tags to format your code, not quote tags. It'll preserve the indentation.

    Yes, you will need to set up the response to selecting an item. But before you can do that, you need to get your stuff showing up in the list. Baby steps.

    I'm not sure why your list is appearing empty, but this is probably where my complete lack of experience with the List component comes in to play. Just to eliminate the simple sources of error, put traces in so you know the name and desc variables are getting set correctly.
    Code:
    function processReport(r:XML):void{
      var name:String = r.@name.toString();
      var desc:String = r.desc.toString();
      trace("name is: "+name);
      trace("desc is: "+desc);
      var report:Report = new Report(name, desc);
      rname.addItem(report);
      reports.push(report);
    }
    That won't fix anything, but it should tell us if we are pulling the right data out. Tell me what the output is.

  7. #27
    Member
    Join Date
    May 2011
    Posts
    39
    okay that also shows nothing, the output box below is empty for some odd reason.

  8. #28
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Ah. your LoadXML (which should be loadXML) never actually gets the XML!
    Code:
    function LoadXML(e:Event):void{
      xmlData = XML(e.target.data);
      var reportNodes:XMLList = xmlData.reportdetails;
      for (var i:int = 0; i < reportNodes.length(); i++){
        var report:XML = reportNodes[i];
        processReport(report);
      }
    }

  9. #29
    Member
    Join Date
    May 2011
    Posts
    39
    ahhh rookie mistake ! well thats what i am anyway ha.

    well now we have a scary mess, when hovering over the list the first field comes up saying : Label
    and if i hover over the List component at all the output shown below is this

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at fl.controls::SelectableList/handleCellRendererMouseEvent()

  10. #30
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You'll have to delve into the actual List stuff then. I'm sure there are a few tutorials around, but I'd just look at the livedocs I linked you earlier. Looks like there's quite a bit of complexity involved with DataProviders and CellRenderers and whatnot, but I'd be surprised if there wasn't a shorthand for the simple case.

    In looking at DataProvider it seems that you can set an XML as the data source directly.

    Seriously, just search for "flash List example". It's what I'd be doing now if I had to do this.

  11. #31
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Found this: http://www.smithmediafusion.com/blog/?p=350
    But that's essentially what we just did.

  12. #32
    Member
    Join Date
    May 2011
    Posts
    39
    yep i did try googling many examples and most do not use xml in their listing! so with my newbieness i really feel overwhelmed by most coding!

    i saw this but it adds the item manually.
    http://www.kirupa.com/developer/mx/listbox.htm

    yes i think in as2 they used dataprovider and labels to populate list items.

  13. #33
    Member
    Join Date
    May 2011
    Posts
    39
    Really though, thank you, thought we were close, was wishing i could go to sleep tonight!.
    i have a working as2 example actually doing what i need! but i do not know how it can be translated into as3 at all as it seems to have changed a lot

  14. #34
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, the link you just posted is for a ListBox, which I think is different.

    There's this: http://www.smithmediafusion.com/blog/?p=337

    But, again, pretty much exactly what we did already.

  15. #35
    Member
    Join Date
    May 2011
    Posts
    39
    oh right, yep. looks very similar. not sure what i am going to do now! it seems like such a simple task right populating a list with items from an XML.

  16. #36
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If I were doing it, I'd use MinimalComps list widget. But I'm sure the fl.components.List is certainly do-able.

    http://www.minimalcomps.com/

  17. #37
    Member
    Join Date
    May 2011
    Posts
    39
    hmm yep. i think i will need to use the fl.components for other tasks, believe it or not the list populating is just one of many things i will be doing on my flash page in the future, right now that is my first step to show my boss tomorrow to see how far i have come. I have been worrying about this all week!

  18. #38
    Member
    Join Date
    May 2011
    Posts
    39
    hey buddy i figured out the listbox all night yesterday! using some google of course!. so i have populated a list box now im so happy!

    One thing i would need from you which you must know is how to now depending on the chosen report... how to make the description change in a text box!!

  19. #39
    Member
    Join Date
    May 2011
    Posts
    39
    this is my code!

    Code:
    function getReportList() { 
    
        var xmlLoader:URLLoader = new URLLoader(); 
        var xmlData:XML = new XML(); 
    
        xmlLoader.addEventListener(Event.COMPLETE, LoadXML); 
        xmlLoader.load(new URLRequest("reports.xml")); 
    
        function LoadXML(e:Event):void { 
    
            xmlData=new XML(e.target.data); 
    
            ParseReports(xmlData); 
    
        } 
    
        function ParseReports(ReportXML:XML):void { 
    
            var ReportList:XMLList=ReportXML.reportdetails.@name; 
             
            rList.removeAll(); 
    
            for each (var reportsTitle:XML in ReportList) { 
    
                rList.addItem({label:reportsTitle}); 
    
            } 
    
        } 
    
    } 
    
    getReportList(); // to populate the info initially 
    setInterval(getReportList, 30000);

    sure you can help me with this part! i am now '''walking'' as you said want to walk abit faster haha
    Last edited by 5TonsOfFlax; 05-25-2011 at 09:25 AM.

  20. #40
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What shows up in your list? You'll probably have to add a data property to your items. Then you'll need to add an event listener for ListEvent.ITEM_CLICK which gets the item from the event and manipulates a textbox.

    To manipulate a TextField, it's pretty easy. Just set the text property.
    Code:
    myTextField.text = "some new content";

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center