2 Replies Latest reply on Feb 19, 2011 9:07 PM by Luis Narea

    Problem with spark List and XML dataProvider

    AWBaumann

      I'm VERY new to Flex so I'm probably doing something wrong, but hopefully that means this will get a quick response.

       

      I'm using a spark list tied to a data provider. On user actions, the list will be filtered. The problem that I'm encountering is that when the result of the xml is a single item it ends up throwing an error:

           Type Coercion failed: cannot convert mx.utils::ObjectProxy to mx.collections.IList.

       

      Here's the code to reproduce the issue...

       

       

      Main.mxml

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application
       xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
       <fx:Script>
       <![CDATA[
        
       protected function singleItemButton_clickHandler(event:MouseEvent):void
       {    
        ItemService.send();    
       }
       protected function mutliItemButton_clickHandler(event:MouseEvent):void
       {
        var dataObject:Object = new Object();
        dataObject.multi = "yes";
        ItemService.send( dataObject );
       }
       ]]>
       </fx:Script>
      
       <fx:Declarations>
        <s:HTTPService    
         id="ItemService" 
         url="http://myserver/xml/itemXML.cfm" />   
       </fx:Declarations>
      
       <s:List id="theList" 
        x="60" y="29" width="200" height="251" 
        dataProvider="{ItemService.lastResult.items.item}" labelField="name"/>
      
       <s:Button x="77" y="287" label="Single Item" click="singleItemButton_clickHandler(event)"/>
       <s:Button x="172" y="287" label="Multi-Item" click="mutliItemButton_clickHandler(event)"/>
      
      </s:Application>
      

       

       

       

       

      itemXML.cfm

      <cfsetting showDebugOutput="No">
      <cfxml variable="result">
       <items>
              <item>
                  <name>Item One</name>
              </item>
        <cfif isDefined('url.multi') AND '#url.multi#' EQ 'yes'>
                  <cfoutput>
                  <item>
                      <name>Item Two</name>
                  </item>
                </cfoutput>
              </cfif>
         </items>
      </cfxml>
      <cfoutput>#result#</cfoutput>
      

       

       

       

        • 2. Re: Problem with spark List and XML dataProvider
          Luis Narea

          If you use spark datagrid or spark List components and you have one record on result in the webService probably you need implements this:

           

          <?xml version="1.0" encoding="utf-8"?>
          <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                                 xmlns:s="library://ns.adobe.com/flex/spark"
                                 xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="prestaService.send()">
              <fx:Declarations>
                  <!-- Place non-visual elements (e.g., services, value objects) here -->
                  <s:HTTPService id="prestaService" method="GET"
                                 url="your URL web service"
                                 result="prestaService_resultHandler(event)"/>
              </fx:Declarations>
             
              <fx:Script>
                  <![CDATA[
                      import mx.collections.ArrayCollection;
                      import mx.events.FlexEvent;
                      import mx.rpc.events.FaultEvent;
                      import mx.rpc.events.ResultEvent;
                      import mx.utils.ArrayUtil;

           

                      protected function prestaService_resultHandler(event:ResultEvent):void
                      {            

           

                              //IF RESULT HAVE 1 RECORD

                              if (prestaService.lastResult.prestashop.products.product.length == undefined){
                              dg.dataProvider = new ArrayCollection(ArrayUtil.toArray(prestaService.lastResult.prestashop.products.product))
                          }

                             //IF RESULT HAVE TWO RECORDS OR MORE
                          else {
                              dg.dataProvider = prestaService.lastResult.prestashop.products.product;
                          }

           

                      }

           

                  ]]>
              </fx:Script>


              <s:DataGrid id="dg">
                  <s:columns>
                      <s:ArrayCollection>
                          <s:GridColumn headerText="Id" dataField="id" width="60"/>
                          <s:GridColumn headerText="Link" dataField="href" width="300"/>
                      </s:ArrayCollection>
                  </s:columns>
              </s:DataGrid>

           

          </s:WindowedApplication>