1 Reply Latest reply on May 13, 2009 3:29 PM by Gregory Lafrance

    using repeater

    rcanulla Level 1

      Hi there,

       

      I'm pulling in XML from yahoo news,  and wondering how I would loop over the results to dynamically create items in a list.

       

      Here is my code.

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="listService.send()" height="534" width="697">
      <mx:Script>
          <![CDATA[
              import mx.collections.XMLListCollection;
              import mx.rpc.events.ResultEvent;
             
              namespace ink = "http://www.inktomi.com/";
              use namespace ink;
             
              [Bindable]
              private var listData:XML;
              [Bindable]
              public var serviceURL:String = "http://boss.yahooapis.com/ysearch/news/v1/biotechnology";   
              private const PROXY:String = "http://localhost/employeePhoneDesktop/src/php/proxy.php?url=";
              private const APIID:String = "################################################";
              private const PARAMS:String = "&format=xml&orderby=date";
             
              private function resultHandler(e:ResultEvent):void {
                  listData = e.result as XML;
                  setData();
              }
              private function setData():void {
                  itemTitle.text = listData..result[0].title;
                  itemDate.text = listData..result[0].date;
                  itemURL.text = listData..result[0].sourceurl;
                  itemDescription.text = listData..result[0].abstract;
                 
              }
          ]]>
      </mx:Script>

       


          <mx:HTTPService id="listService" url="{serviceURL + APIID + PARAMS}" result="resultHandler(event)" resultFormat="e4x"/>
         
          <mx:TextArea text="{listData}" x="309" y="312" width="339" height="212"/>  
              <mx:VBox verticalGap="0">   
                  <mx:Label id="itemTitle"  fontWeight="bold" fontSize="12" paddingBottom="0"/>
                  <mx:Label id="itemURL" />
                  <mx:Label id="itemDate" />
                  <mx:Text id="itemDescription" width="300"  />
              </mx:VBox>    

      </mx:Canvas>

        • 1. Re: using repeater
          Gregory Lafrance Level 6

          If you get the data back as well formed XML, then just stuff it in an XMLListCollection and use e4x to access the data within as you desire.

           

          Here is a somewhat generic HTTPService example that may be of help:

           

          ---------------- mainapp.mxml ---------------

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="dataRequest.send();filterDataGrid();">
            <mx:Script>
              <![CDATA[
                import mx.rpc.events.ResultEvent;
                import mx.collections.XMLListCollection;
                import mx.controls.CheckBox;
                
                [Bindable] private var xlc:XMLListCollection;
                [Bindable] private var displayedxlc:XMLListCollection;
                [Bindable] private var categories:XMLListCollection;
                
                private function processResult(evt:ResultEvent):void{
                  xlc = new XMLListCollection(evt.result..item as XMLList);
                  categories = new XMLListCollection(evt.result..cat);
                }
                
                private function filterDataGrid():void{
                  var tempxlc:XMLListCollection = new XMLListCollection();
                  for each(var xml:XML in xlc){
                    for each( var cb:CheckBox in chbx){
                      if(xml.category == cb.label && cb.selected){
                        tempxlc.addItem(xml);
                      }          
                    }
                  }
                  displayedxlc = tempxlc;
                }
              ]]>
            </mx:Script>
          
            <mx:HTTPService id="dataRequest" url="data.xml" resultFormat="e4x"
              result="processResult(event)"/> 
            <mx:HBox>
              <mx:Repeater id="rp" dataProvider="{categories}">
                <mx:CheckBox id="chbx" label="{rp.currentItem}" change="filterDataGrid()"/>
              </mx:Repeater>
            </mx:HBox> 
            <mx:List dataProvider="{displayedxlc}" width="300" height="300">
              <mx:itemRenderer>
                <mx:Component>
                  <mx:Label text="{data.food}"/>
                </mx:Component>
              </mx:itemRenderer>
            </mx:List>
          </mx:Application>
          
          

           

          -------------- data.xml ----------------

          <?xml version="1.0" encoding="utf-8"?>
          <foods>
            <categories>
              <cat>vegetable</cat>
              <cat>meat</cat>
              <cat>beverage</cat>
              <cat>dessert</cat>
            </categories>
            <item>
              <category>vegetable</category>
              <food>beans</food>
            </item>
            <item>
              <category>vegetable</category>
              <food>carrots</food>
            </item>
            <item>
              <category>vegetable</category>
              <food>pepper</food>
            </item>
            <item>
              <category>meat</category>
              <food>beef</food>
            </item>
            <item>
              <category>meat</category>
              <food>chicken</food>
            </item>
            <item>
              <category>meat</category>
              <food>pork</food>
            </item>
            <item>
              <category>beverage</category>
              <food>milk</food>
            </item>
            <item>
              <category>beverage</category>
              <food>juice</food>
            </item>
            <item>
              <category>beverage</category>
              <food>soda</food>
            </item>
            <item>
              <category>dessert</category>
              <food>cake</food>
            </item>
            <item>
              <category>dessert</category>
              <food>cookie</food>
            </item>
            <item>
              <category>dessert</category>
              <food>pie</food>
            </item>
          </foods>