2011-08-05 47 views
0

我想從我的SQLite獲取任何在Flex中呈現,我無法弄清楚如何在Flex中顯示任何數據,甚至文本。我究竟做錯了什麼?在Flex幫助中的SQLite

<fx:Script> 
    <![CDATA[ 
     import flash.data.SQLConnection; 
     import flash.data.SQLResult; 
     import flash.data.SQLStatement; 
     import flash.filesystem.File; 

     import mx.collections.ArrayCollection; 

     private var conn:SQLConnection; 
     private var createStmt:SQLStatement; 
     private var selectStmt:SQLStatement; 
     [bindable] public var dataField:ArrayCollection; 
     [bindable] private var row:Object; 
     [bindable] private var pngIndex:int; 
     [bindable] public var pngTitle:String; 
     [bindable] private var pngByteArray:ByteArray; 
     [Bindable] private var dp:ArrayCollection = new ArrayCollection(); 

     private function init():void 
     { 
      conn = new SQLConnection(); 
      conn.addEventListener (SQLEvent.OPEN, openSuccess); 
      conn.addEventListener (SQLErrorEvent.ERROR, openFailure); 

      var dbFile:File = File.applicationDirectory.resolvePath("assets/NM.sqlite"); 
      conn.openAsync(dbFile); 
     } 

     private function openSuccess(event:SQLEvent):void 
     { 
      conn.removeEventListener(SQLEvent.OPEN, openSuccess); 
      conn.removeEventListener(SQLErrorEvent.ERROR, openFailure); 

      getData(); 
     } 


     private function getData():void 
     { 
      //status = "Loading data"; 

      selectStmt = new SQLStatement(); 
      selectStmt.sqlConnection = conn; 
      var sql:String = "SELECT Title FROM Data WHERE 'Index' = 0"; 
      selectStmt.text = sql; 

      selectStmt.addEventListener(SQLEvent.RESULT, selectResult); 
      selectStmt.addEventListener(SQLErrorEvent.ERROR, selectError); 

      selectStmt.execute(); 
     } 

     private function selectResult(event:SQLEvent):void 
     { 
      //status = "Data loaded"; 

      selectStmt.removeEventListener(SQLEvent.RESULT, selectResult); 
      selectStmt.removeEventListener(SQLErrorEvent.ERROR, selectError); 

      [bindable] var result:SQLResult = selectStmt.getResult(); 
      dataField = new ArrayCollection(result.data); 
      dp = ArrayCollection(dataField); 

      if (dataField != null) { 
       pngIndex = result.data.Index; 
       pngTitle = result.data.Title; 
       pngByteArray = result.data.Picture; 

       /* Pic.source = pngByteArray; */ 

      } 

     } 

    ]]> 
</fx:Script> 
<s:List x="31" y="44" width="511" height="415" dataProvider="{dp}"></s:List> 

我已經嘗試了許多不同的組件,而我似乎無法獲得數據正確綁定(儘管此代碼終於沒有可怕的「數據綁定將無法檢測分配......「消息,或其他可怕的」屬性未定義「消息)。

請幫忙!

+0

嘗試將'dataField'綁定到'List'的'dataProvider'而不是'dp'。 – NoobsArePeople2

+0

當我嘗試時,我得到「數據綁定將無法檢測到'dataField'的分配」錯誤。 另一個常見的喜愛是1067:隱式強制。哎呀。 – SQLiteNoob

+0

'[bindable]'的所有實例都需要'[Bindable]'。 – NoobsArePeople2

回答

1

固定。

<fx:Script> 
    <![CDATA[ 
     import flash.data.SQLConnection; 
     import flash.data.SQLResult; 
     import flash.data.SQLStatement; 
     import flash.filesystem.File; 

     import mx.collections.ArrayCollection; 

     private var conn:SQLConnection; 

     private function init():void 
     { 
      conn = new SQLConnection(); 
      conn.addEventListener(SQLEvent.OPEN, openSuccess); 
      conn.addEventListener(SQLErrorEvent.ERROR, openFailure); 
      var dbFile:File = File.applicationDirectory.resolvePath("assets/NM.sqlite"); 
      conn.openAsync(dbFile); 
     } 

     private function openSuccess(event:SQLEvent):void 
     { 
      conn.removeEventListener(SQLEvent.OPEN, openSuccess); 
      conn.removeEventListener(SQLErrorEvent.ERROR, openFailure); 
      getData(); 
     } 

     private function getData():void 
     { 
      var select:SQLStatement = new SQLStatement(); 
      select.sqlConnection = conn; 
      select.text = "SELECT Title FROM Data WHERE 'Index' = 0"; 
      select.addEventListener(SQLEvent.RESULT, selectResult); 
      select.addEventListener(SQLErrorEvent.ERROR, selectError); 
      select.execute(); 
     } 

     private function selectResult(event:SQLEvent):void 
     { 
      var result:SQLResult = event.currentTarget.getResult(); 
      if(result.data) 
      { 
       list.dataProvider = new ArrayCollection(result.data); 
      } 
     } 

    ]]> 
</fx:Script> 
<s:List id="list" x="31" y="44" width="511" height="415" labelField="Title" /> 

我還建議您詳細瞭解Flex如何處理列表和項目渲染器。這個代碼有很多問題(即是錯誤的,因爲data是一個數組)。從我可以收集的內容中,您將需要一個自定義項目渲染器。此外,如果你不需要它,我不建議你使用Binding(因爲這個例子完全),因爲它會產生額外的資源。

+0

是的,我發佈的代碼只是一個工作表 - 我知道result.data [0] .Index是我想要的正確語法,但我拉着我的頭髮,因爲一切看起來很好,而且果然是,在我首先找到了我失蹤的方法。 我聽說你有約束力。 – SQLiteNoob