具有itemrenderer的简单数据组无法在flex mobile中运行

时间:2014-03-05 06:20:52

标签: actionscript-3 flex flex4.5 flex-mobile

我正在使用数据组来显示XML数据。当我使用默认项目渲染器时,节点正在显示,但是当我尝试使用自己的itemrenderer时,它会失败。这是代码:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">
    <s:layout>
        <s:VerticalLayout paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.collections.XMLListCollection;
        ]]>
    </fx:Script>
    <fx:Declarations>
        <fx:XML id="DataXml" source="Data.xml"/>
    </fx:Declarations>
    <s:Label text="Start"/>
    <s:Scroller>
        <s:DataGroup width="100%" height="100%" itemRenderer="views.DataItemRenderer"
                 dataProvider="{new XMLListCollection(DataXml.children())}" >
        <s:layout>
            <s:VerticalLayout/>
        </s:layout>
    </s:DataGroup>

    </s:Scroller>

    <s:Label text="end"/>
</s:View>

项目渲染器::::

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                    xmlns:s="library://ns.adobe.com/flex/spark"
                    width="100%"   >
<fx:Declarations>
    <s:HGroup width="100%">
        <s:Label id="txtFirstName"  text="Name::"/>
        <s:Label id="locationTxt" text="LOcation::"/>
        <s:Label id="packTxt"  text="Package::"/>
        <s:Label id="experienceTxt"   text="Experience::"/>
        <s:Label id="designationTxt"   text="Designation::"/>

    </s:HGroup>

</fx:Declarations>

    <fx:Script>
        <![CDATA[
            override public function set data(value:Object):void {
                txtFirstName.text += value.firstName;
                locationTxt.text += value.location;
                packTxt.text += value.pack;
                experienceTxt.text += value.experience;
                designationTxt.text += value.designation;
            }
        ]]>
    </fx:Script>
</s:ItemRenderer>

XML File ::

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <employee>
        <firstname>Bujji</firstname>
        <location>Hyderabad</location>
        <pack>28000</pack>
        <experience>1year</experience>
        <designation>ASE</designation>
    </employee>

    <employee>
        <firstname>Tanuja</firstname>
        <location>Hyderabad</location>
        <pack>25000</pack>
        <experience>2Year</experience>
        <designation>ASE</designation>
    </employee>

    <employee>
        <firstname>Sarath</firstname>
        <location>Banglore</location>
        <pack>12000</pack>
        <experience>1Year</experience>
        <designation>JSE</designation>
    </employee>
</data>

2 个答案:

答案 0 :(得分:0)

问题出在ItemRenderer文件中。您已在s:HGroup标记内找到了fx:Declerations。把它放在外面,应该没问题。

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                    xmlns:s="library://ns.adobe.com/flex/spark"
                    width="100%"   >
    <s:HGroup width="100%">
        <s:Label id="txtFirstName"  text="Name::"/>
        <s:Label id="locationTxt" text="LOcation::"/>
        <s:Label id="packTxt"  text="Package::"/>
        <s:Label id="experienceTxt"   text="Experience::"/>
        <s:Label id="designationTxt"   text="Designation::"/>

    </s:HGroup>
</s:ItemRenderer>

NB: declerations标记用于非可视元素。 (See adobe docs

答案 1 :(得分:0)

以下项目渲染器适合我:

screenshot

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                    xmlns:s="library://ns.adobe.com/flex/spark"
                    width="100%" height="20">
    <s:HGroup width="100%">
        <s:Label id="txtFirstName"/>
        <s:Label id="locationTxt"/>
        <s:Label id="packTxt"/>
        <s:Label id="experienceTxt"/>
        <s:Label id="designationTxt"/>
    </s:HGroup>

    <fx:Script>
        <![CDATA[
            override public function set data(value:Object):void {
                super.data = value;

                trace("Name: " + value.firstname);

                txtFirstName.text = "Name: " + value.firstname;
                locationTxt.text = "Location: " + value.location;
                packTxt.text = "Package: " + value.pack;
                experienceTxt.text = "Experience: " + value.experience;
                designationTxt.text = "Designation: " + value.designation;
            }
        ]]>
    </fx:Script>
</s:ItemRenderer>