0

我有一个当前包含 2 个视图的 Flex 移动应用程序

视图 1 有一个使用 itemRenderer 显示 ID 的数据绑定列表,而标签视图 2 显示一个使用 itemRenderer 显示基于数据集内容的控件的数据绑定列表。

所有数据都保存在 SQLite 数据库中。

视图 1 是调查列表 视图 2 是与每个调查相关的问题列表。

我正在努力的是将调查 ID 值传递给视图 2 中的 SQL 查询,以便返回的数据是与每个调查相关的问题。

我一遍又一遍的 dispatchEvent 但无法弄清楚如何将相关值从 data.surveyID 传递到视图 2 上的参数变量以查询数据库。

查看 1 个代码

                <?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" 
                    xmlns:mx="library://ns.adobe.com/flex/mx" 
                    title="Surveys"
                    creationComplete="listen()">
                <fx:Script>
                    <![CDATA[
                        import flash.data.SQLConnection;
                        import flash.data.SQLResult;
                        import flash.data.SQLStatement;
                        import flash.events.Event;
                        import flash.filesystem.File;
                        import mx.collections.ArrayCollection;
                        import spark.events.IndexChangeEvent;
                        import spark.events.ListEvent;
                        import spark.transitions.FlipViewTransition;
                        import spark.transitions.FlipViewTransitionMode;
                        import spark.transitions.ViewTransitionDirection;
                        private var conn:SQLConnection;
                        private function get myData():ArrayCollection 
                        {
                            var stmt:SQLStatement = new SQLStatement();
                            stmt.sqlConnection = new SQLConnection();
                            stmt.sqlConnection.open(File.applicationDirectory.resolvePath("assets/cp_db.sqlite"));
                            stmt.text = "SELECT * FROM surveys";
                            stmt.execute();
                            var result:Array = stmt.getResult().data;
                            if (result)
                            {
                                var list:ArrayCollection = new ArrayCollection();
                                for (var i:int=0; i<result.length; i++) 
                                {
                                    list.addItem(result[i]);
                                }
                                return list; 
                            }
                            else 
                            {
                                return null; 
                            } 
                        }
                        private function workingHandler ( event:Event ) : void
                        {
                            trace( "I'm the working handler here :) " );
                        }
                        private function listen():void{
                        list.addEventListener( "switchToDay", regularListener );

                        }

                        public var itemLabel:String;
                        private function regularListener (event:Event) : void
                        {
                            trace( "I'm the regular listener"  );
                            trace(itemLabel);
                        }
                        public function conIndex( ind:int):String{
                            return ind.toString();
                        }

                    ]]>

                </fx:Script>
                <fx:Declarations>
                    <!-- Place non-visual elements (e.g., services, value objects) here -->
                </fx:Declarations>

                <s:List id="list" left="10" right="10" top="10" bottom="10" labelField="{data.SurveyName}"
                        click="navigator.pushView(surveyDetailView, conIndex(list.selectedIndex+1));"
                        dataProvider="{myData}"  >
                    <s:itemRenderer>
                        <fx:Component>
                            <s:ItemRenderer>
                                <s:LabelItemRenderer id="surID" width="50" label="{data.surveyID}"/>
                                <s:LabelItemRenderer id="surName" x="0" width="100%" label="{data.SurveyName}"
                                                     click="surName.dispatchEvent(new Event('switchToDay', true,false));"/>
                            </s:ItemRenderer>
                        </fx:Component>
                    </s:itemRenderer>
                </s:List>
            </s:View>

查看 2 代码

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx"
    title="{data}">
<fx:Declarations>
    <s:SlideViewTransition id="slideTrans" direction="down" duration="300"/>
</fx:Declarations>
<fx:Script>
    <![CDATA[
        import flash.data.SQLConnection;
        import flash.data.SQLStatement;
        import flash.filesystem.File;
        import flash.filesystem.FileMode;
        import mx.collections.ArrayCollection;
        import spark.components.Button;
        import spark.components.Label;
        public var surveyIdParam:String = '1';
        public var myPar:String;
        public function get myData():ArrayCollection 
        {
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = new SQLConnection();
            stmt.sqlConnection.open(File.applicationDirectory.resolvePath("assets/cp_db.sqlite"));

            stmt.parameters[0] = surveyIdParam;
            stmt.text = "SELECT * FROM surveyquestions  where surveyID = ?";
            stmt.execute();
            var result:Array = stmt.getResult().data;

            if (result)
            {
                var list:ArrayCollection = new ArrayCollection();
                for (var i:int=0; i<result.length; i++) 
                {
                    list.addItem(result[i]);
                }
                return list; 
            }
            else 
            {
                return null; 
            } 
        }
    ]]>
</fx:Script>
<s:List id="list" left="0" right="0" top="50" bottom="0" dataProvider="{myData} ">
    <s:itemRenderer>
        <fx:Component>
            <s:ItemRenderer>
                <fx:Script>
                    <![CDATA[
                        import spark.components.Label;
                        private function addItem(qtype:String):void
                        {

                            if (qtype == "1") {
                                icrHeading.visible = true;
                                ti.visible=false;
                                cb.visible=false;
                            }
                            else if (qtype == "2") {

                            }
                            else if (qtype == "3") {
                                icrDetail.visible=true;
                            }
                        }
                    ]]>
                </fx:Script>
                <s:VGroup id="contentHolder">

                </s:VGroup>

                <s:IconItemRenderer id="icr1" visible="false" x="0" width="50"
                                    label="{data.QuestionTypeID}"/> 
                <s:IconItemRenderer id="icrHeading" visible="false" x="0" width="450"
                                    label="{data.QuestionTitle}" color="0xff0000"/>
                <s:IconItemRenderer id="icrDetail" visible="false" x="50" width="450"
                                    label="{data.QuestionTitle}"/>
                <s:Label id="ilbl1" x="50" width="120" added="addItem(icr1.label.toString())"/>
                <s:TextInput id="ti" x="600" width="200" text=""/>
                <s:CheckBox id="cb" x="550">
                </s:CheckBox>
            </s:ItemRenderer>
        </fx:Component>
    </s:itemRenderer>
</s:List> 
<s:Button id="btn" x="717" y="10" label="{data}" click="navigator.popView()"/>
4

1 回答 1

0

本文可能会帮助您在视图之间传递数据。基本上,navigator.pushView接受参数

于 2012-09-24T20:23:09.327 回答