views:

295

answers:

1

I have a page (below) that has a datagrid that lists "item"'s returned from an XML file (below) and when it loads it created a page in the viewstack for each item it finds (working) it also renders a page (below) inside each panel but i am having issues passing the data. Each page renders and loads but with the FIRST result of the XML data instead of each page having its own data they all have the same as the first page.

HomePage.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
 width="100%" height="100%"
 creationComplete="init()"
 xmlns:software="com.xd.components.pages.software.*"
 xmlns:pages="com.xd.components.pages.*"
xmlns:renderers="com.xd.components.renderers.*">
<mx:Script>
<![CDATA[
import mx.core.Application;
import com.xd.components.renderers.PageListRenderer;
import mx.containers.Panel;
import mx.core.Container;
import com.xd.components.renderers.SoftwareListRenderer;
import mx.utils.ObjectProxy;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.containers.VBox;
[Bindable] public var httpData:Object;
[Bindable] public var selectedItem:String;

        private function init():void {
            //get data
            httpService.send();    
        }

        //private const MAX_CHILDREN:uint = 5;

        private function viewstack_addChild(name:String):void {
            var p:Panel = new Panel();
            p.id = name;
            p.name = name;
            p.title = name;
            p.percentWidth = 100;
            p.percentHeight = 100;
            var randColor:uint = Math.random() * 0xFFFFFF;
            p.setStyle("backgroundColor", randColor);
            var pR:PageListRenderer = new PageListRenderer();
            //var data:Object;
            //Do something to get the data to be displayed;
            //data = httpService.lastResult.item;
            pR.data = httpData;
            p.addChild(pR);
            myViewStack.addChild(p);
        }
        private function httpResult_handler(evt:ResultEvent):void {
            if (evt.result.software.item) {

             httpData = XML(evt.result).descendants("item");
             var item:Object = httpData;
             for each(item in httpData) {
              viewstack_addChild(item.name);
             }
            }
        }
     public function switchView(viewId:String):void
     {
         var container:Container = Container(myViewStack.getChildByName(viewId));
         selectedItem = viewId;
         if (container != null)
         {
             myViewStack.selectedChild = container;
         }
     }
    ]]>
</mx:Script>

<mx:HTTPService id="httpService" url="data/Software.xml" resultFormat="e4x" result="httpResult_handler(event)" fault="Alert.show('XML Data Error')" />

<mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<mx:HBox width="100%" height="100%">
<mx:VBox width="30%" height="100%">
<mx:Panel borderThicknessLeft="0" borderThicknessRight="0" borderThicknessBottom="0" width="100%" height="100%" title="Software List:">
    <mx:DataGrid id="myDG" 
                dataProvider="{httpData}"
                headerHeight="0"
                editable="false"
                width="100%" height="100%" 
                rowHeight="50"
                itemClick="switchView(myDG.selectedItem.name);">

        <mx:columns>
            <mx:DataGridColumn itemRenderer="com.xd.components.renderers.SoftwareListRenderer" />
        </mx:columns>

    </mx:DataGrid>
</mx:Panel>
</mx:VBox>
<mx:VBox width="70%" height="100%">
 <mx:ViewStack id="myViewStack" width="100%" height="100%" hideEffect="WipUp" showEffect="WipeDown" creationPolicy="all">
   <pages:NewsPage id="NewsPage" height="100%" width="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" />
   <software:ShowTracker id="ShowTracker" height="100%" width="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" />
 </mx:ViewStack>
</mx:VBox>
</mx:HBox>
</mx:VBox>
</mx:Canvas>

PageListRenderer.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:HBox 
height="100%" 
width="100%" 
xmlns:mx="http://www.adobe.com/2006/mxml"&gt;

<mx:Script>
 <![CDATA[
  import mx.core.Application;
 ]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:VBox height="70%" width="100%" horizontalAlign="center" verticalAlign="middle">
  <mx:Image source="{data.image}"  width="90%" height="90%"/>
</mx:VBox>
 <mx:VBox height="30%" width="100%" horizontalAlign="left" verticalAlign="top">
  <mx:Label width="100%" text="Name / Version:" color="#FFFFFF"/>
     <mx:Label width="100%" text="{data.name}" color="#FFAE00"/>
     <mx:Label width="100%" text="{data.version}" color="#FFAE00"/>
     <mx:Label width="100%" text="{data.longdesc}" color="#FFFFFF"/>
<mx:Button click="installApp()" label="Install Application" horizontalCenter="0" verticalCenter="0" width="160" height="52" id="install" visible="false"/>
  <mx:Button click="launchApp()" label="Launch Application" horizontalCenter="0" verticalCenter="0" width="160" height="52" id="launch" visible="false"/>
 </mx:VBox> 
</mx:VBox> 
</mx:HBox>

Software.xml

<software>

<item>
<name>ShowTracker</name>
<version>1.0</version>
<shortdesc>TV Show Tracker.</shortdesc>
<image></image>
<longdesc>ShowTracker allows you to manage all the TV Shows you watch. I helps you keep track of what episode you are upto for each series you are currently watching</longdesc>
<appid>com.showtracker</appid>
<pubid>EB6DA1B683461BF1C71FEC13E276982941DF167B.1</pubid>
<appurl>http://www.nitcom.com.au/showtracker/downloads/ShowTracker-v1.air&lt;/appurl&gt;
<runversion>1.5</runversion>
</item>
<item>
<name>Test2</name>
<version>1</version>
<shortdesc>test desc #2</shortdesc>
<image>test1.jpg</image>
<longdesc>test1.jpg</longdesc>
<appid>com.showtracker</appid>
<pubid>EB6DA1B683461BF1C71FEC13E276982941DF167B.1</pubid>
<appurl>http://www.nitcom.com.au/showtracker/downloads/ShowTracker-v1.air&lt;/appurl&gt;
<runversion>1.5</runversion>
</item>
<item>
<name>Test #3</name>
<version>1</version>
<shortdesc>test desc #3</shortdesc>
<image>test1.jpg</image>
<longdesc>test1.jpg</longdesc>
<appid>com.showtracker</appid>
<pubid>EB6DA1B683461BF1C71FEC13E276982941DF167B.1</pubid>
<appurl>http://www.nitcom.com.au/showtracker/downloads/ShowTracker-v1.air&lt;/appurl&gt;
<runversion>1.5</runversion>
</item>
</software>
A: 

dont know if this is causing the problem but it looks like this :

private function httpResult_handler(evt:ResultEvent):void {
     if (evt.result.software.item) {

       httpData = XML(evt.result).descendants("item");
       var item:Object = httpData;
       for each(item in httpData) {
          viewstack_addChild(item.name);
       }
    }
 }

needs to be this:

private function httpResult_handler(evt:ResultEvent) :void {
  if(evt.result.software.item){
    var httpData:XMLList = XML(evt.result).descendants("item");
    for each(var item:Object in httpData){
      viewstack_addChild(item.name);
    }
  }
}

and really you should pass the view stack the whole item and let it take what it needs instead of sending it individual properties.

greg
Hi Greg, thank you for the response am i not doing that in "private function viewstack_addChild(name:String):void" where pR.data = httpData ? or is that part incorrect? the function you are referring to is only sending it the name so it can create each page with that unique name.
medoix
The version you have pasted has issues where the variable i had originally for httpData was a bindable Object and therefore this breaks the whole lot, if i change the var httpData:XMLList to just httpData and change the variable at the top to XMLList it goes back to the way it was working before, data grid is fine but pages all have the same content.
medoix
After some work you are correct, instead of just passing it item.name i am now passing the whole item and i changed the viewstack_addChild(name:String) function to viewstack_addChild(item:Object) so i can now use all the data correctly. Thank you Greg.
medoix