2012-03-11 9 views
1

私はdataProviderを使用してListコントロールにデータを表示する方法を知っていますが、dataProviderを持たないコントロールについてはどうでしょうか? TextInput、Label contorl?ArrayCollection(SQLite db)を使用してdataProviderを使用しないコントロールにデータを表示する方法は?

私はdataProviderを使用してListコントロールにデータを表示する方法を知っていますが、dataProviderを持たないコントロールはどうでしょうか? TextInput、Label contorl?

<?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" 
      creationComplete="view1_creationCompleteHandler(event)" > 

     <fx:Script> 
      <![CDATA[ 


import mx.collections.ArrayCollection; 
       import mx.events.FlexEvent; 

       private var db:File = File.userDirectory.resolvePath("users01.db"); 
       private var conn:SQLConnection; 
       private var Stmt:SQLStatement; 

       [Bindable]private var dp:ArrayCollection = new ArrayCollection(); 

       protected function view1_creationCompleteHandler(event:FlexEvent):void   { 
        conn = new SQLConnection(); 
        conn.addEventListener(SQLEvent.OPEN, openHandler); 
        conn.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
        conn.openAsync(db); 
       } 
       private function openHandler(event:SQLEvent):void { 
        log.text += "Database opened successfully"; 
        conn.removeEventListener(SQLEvent.OPEN, openHandler); 
        Stmt = new SQLStatement(); 
        Stmt.sqlConnection = conn; 
        Stmt.text = "CREATE TABLE IF NOT EXISTS UserTable (" + 
         "userId INTEGER PRIMARY KEY AUTOINCREMENT," + 
         "userLevel," + 
         "firstName TEXT," + "lastName TEXT)"; 
        Stmt.addEventListener(SQLEvent.RESULT, createResult); 
        Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
        Stmt.execute(); 
       } 
       private function createResult(event:SQLEvent):void { 
        log.text += "\nTable created"; 
        conn.removeEventListener(SQLEvent.RESULT, createResult); 
        showResult(); 
       } 
       private function errorHandler(event:SQLErrorEvent):void { 
        log.text += "\nError message: " + event.error.message; 
        log.text += "\nDetails: " + event.error.details; 
       } 
       private function insertResult(event:SQLEvent):void { 
        log.text += "\nInsert completed"; 
        showResult(); 
       } 
       private function showResult():void { 
        Stmt = new SQLStatement(); 
        Stmt.sqlConnection = conn; 
        Stmt.text = "SELECT * FROM UserTable"; 
        Stmt.addEventListener(SQLEvent.RESULT, selectResult); 
        Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
        Stmt.execute(); 
       } 
       private function selectResult(event:SQLEvent):void { 
        log.text += "\nSelect completed"; 
        var result:SQLResult = Stmt.getResult(); 
        listBox.dataProvider=new ArrayCollection(result.data); 
       } 
       protected function Save(event:MouseEvent):void   { 
        Stmt = new SQLStatement(); 
        Stmt.sqlConnection = conn; 
        Stmt.text = "INSERT INTO UserTable (firstName, lastName, userLevel)" + 
         "VALUES (:firstName, :lastName, :userLevel)"; 
        Stmt.parameters[":firstName"] = firstName.text; 
        Stmt.parameters[":lastName"] = lastName.text; 
        Stmt.parameters[":userLevel"] = ID.text; 
        Stmt.addEventListener(SQLEvent.RESULT, insertResult); 
        Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
        Stmt.execute(); 
       } 
       protected function Delete(event:MouseEvent):void { 

        Stmt = new SQLStatement(); 
        Stmt.sqlConnection = conn; 
        Stmt.text="DELETE FROM UserTable WHERE lastName = ?"; 
        Stmt.parameters[0] = listBox.selectedItem.lastName; 
        Stmt.addEventListener(SQLEvent.RESULT, insertResult); 
        Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
        Stmt.execute(); 
        trace("record deleted"); 
       } 
       protected function Update(event:MouseEvent):void   { 
        Stmt = new SQLStatement(); 
        Stmt.sqlConnection = conn; 
        Stmt.text = "UPDATE UserTable SET " + 
         "firstName=:firstName, " + 
         "lastName = :lastName " + 
         "WHERE userLevel = :userLevel2"; 
        Stmt.parameters[":firstName"] = firstName.text; 
        Stmt.parameters[":lastName"] = lastName.text; 
        Stmt.parameters[":userLevel2"] = ID.text; 
        Stmt.addEventListener(SQLEvent.RESULT, insertResult); 
        Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
        Stmt.execute(); 
       } 
      ]]> 
     </fx:Script> 


     <s:Label text="FirstName" top="8" left="5"/> 
     <s:TextInput id="firstName" left="-1" top="36" width="233" height="80"/> 

     <s:Label text="LastName" top="0" left="248"/> 
     <s:TextInput id="lastName" left="231" top="32" width="224" height="84"/> 

     <s:Label text="ID" top="0" left="483"/> 
     <s:TextInput id="ID" left="463" top="32" width="167" height="84"/> 

     <s:Button left="10" top="151" width="121" height="54" label="Save" click="Save(event)"/> 
     <s:Button left="139" top="151" width="146" height="54" label="Delete" click="Delete(event)" enabled="{listBox.selectedIndex != -1}"/> 
     <s:Button left="293" top="151" width="147" height="54" label="Update" click="Update(event)"/> 

     <s:List id="listBox" x="-1" y="265" width="641" height="167" itemRenderer="UserRenderer"></s:List> 

     <s:TextArea id="log" x="0" bottom="4" width="100%" height="221"/> 
    </s:View> 

のTextInputに{dp.ID}{dp.getItemAt(0).ID}を使用してみてくださいが、何も表示されませんか?

ありがとうございました。

作成方法TextInput id = "firstName"はSQLデータを表示できますか?

答えて

1

TextInputオブジェクトに値を代入するか、別のバインド可能な文字列変数を使用して、バインドしてメソッドに代入する必要があると思います。コンパイル時に、私が信じているgetItemAtを実行したときにその行にバインディングを使用できないという警告が表示されます。 dataproviderが配列コレクションとして型指定され、コレクション自体にIDプロパティがないため、dp.IDは意味をなさないでしょう。バインディングは基本的に、プロパティのセッターが呼び出されるたびにイベントディスパッチを作成し、mxmlコンパイラはバインダーをマークするプロパティのセッターとゲッターを追加し、セッターが呼び出され、その値が持つイベントを自動的にディスパッチしますかわった。そのイベントはキャプチャされ、そのイベントにバインドされているものに設定されます。私は以下のコードをコピーして変更:(もう一度上からあなたのコードを貼り付け、私は以下を参照してください何の二つのオプション)

編集:私はもともと見たものに基づいて

オプション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" title="HomeView" 
     creationComplete="view1_creationCompleteHandler(event)" > 

    <fx:Script> 
     <![CDATA[ 


      import mx.collections.ArrayCollection; 
      import mx.events.FlexEvent; 

      private var db:File = File.userDirectory.resolvePath("users01.db"); 
      private var conn:SQLConnection; 
      private var Stmt:SQLStatement; 

      [Bindable]private var dp:ArrayCollection = new ArrayCollection(); 

      protected function view1_creationCompleteHandler(event:FlexEvent):void   { 
       conn = new SQLConnection(); 
       conn.addEventListener(SQLEvent.OPEN, openHandler); 
       conn.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       conn.openAsync(db); 
      } 
      private function openHandler(event:SQLEvent):void { 
       log.text += "Database opened successfully"; 
       conn.removeEventListener(SQLEvent.OPEN, openHandler); 
       Stmt = new SQLStatement(); 
       Stmt.sqlConnection = conn; 
       Stmt.text = "CREATE TABLE IF NOT EXISTS UserTable (" + 
        "userId INTEGER PRIMARY KEY AUTOINCREMENT," + 
        "userLevel," + 
        "firstName TEXT," + "lastName TEXT)"; 
       Stmt.addEventListener(SQLEvent.RESULT, createResult); 
       Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       Stmt.execute(); 
      } 
      private function createResult(event:SQLEvent):void { 
       log.text += "\nTable created"; 
       conn.removeEventListener(SQLEvent.RESULT, createResult); 
       showResult(); 
      } 
      private function errorHandler(event:SQLErrorEvent):void { 
       log.text += "\nError message: " + event.error.message; 
       log.text += "\nDetails: " + event.error.details; 
      } 
      private function insertResult(event:SQLEvent):void { 
       log.text += "\nInsert completed"; 
       showResult(); 
      } 
      private function showResult():void { 
       Stmt = new SQLStatement(); 
       Stmt.sqlConnection = conn; 
       Stmt.text = "SELECT * FROM UserTable"; 
       Stmt.addEventListener(SQLEvent.RESULT, selectResult); 
       Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       Stmt.execute(); 
      } 
      private function selectResult(event:SQLEvent):void { 
       log.text += "\nSelect completed"; 
       var result:SQLResult = Stmt.getResult(); 
       listBox.dataProvider=new ArrayCollection(result.data); 
       if(result.data.length>0) //I ADDED THIS 
        firstName.text = result.data[0].firstName; //I ADDED THIS 
      } 
      protected function Save(event:MouseEvent):void   { 
       Stmt = new SQLStatement(); 
       Stmt.sqlConnection = conn; 
       Stmt.text = "INSERT INTO UserTable (firstName, lastName, userLevel)" + 
        "VALUES (:firstName, :lastName, :userLevel)"; 
       Stmt.parameters[":firstName"] = firstName.text; 
       Stmt.parameters[":lastName"] = lastName.text; 
       Stmt.parameters[":userLevel"] = ID.text; 
       Stmt.addEventListener(SQLEvent.RESULT, insertResult); 
       Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       Stmt.execute(); 
      } 
      protected function Delete(event:MouseEvent):void { 

       Stmt = new SQLStatement(); 
       Stmt.sqlConnection = conn; 
       Stmt.text="DELETE FROM UserTable WHERE lastName = ?"; 
       Stmt.parameters[0] = listBox.selectedItem.lastName; 
       Stmt.addEventListener(SQLEvent.RESULT, insertResult); 
       Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       Stmt.execute(); 
       trace("record deleted"); 
      } 
      protected function Update(event:MouseEvent):void   { 
       Stmt = new SQLStatement(); 
       Stmt.sqlConnection = conn; 
       Stmt.text = "UPDATE UserTable SET " + 
        "firstName=:firstName, " + 
        "lastName = :lastName " + 
        "WHERE userLevel = :userLevel2"; 
       Stmt.parameters[":firstName"] = firstName.text; 
       Stmt.parameters[":lastName"] = lastName.text; 
       Stmt.parameters[":userLevel2"] = ID.text; 
       Stmt.addEventListener(SQLEvent.RESULT, insertResult); 
       Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       Stmt.execute(); 
      } 
     ]]> 
    </fx:Script> 


    <s:Label text="FirstName" top="8" left="5"/> 
    <s:TextInput id="firstName" left="-1" top="36" width="233" height="80"/> 

    <s:Label text="LastName" top="0" left="248"/> 
    <s:TextInput id="lastName" left="231" top="32" width="224" height="84"/> 

    <s:Label text="ID" top="0" left="483"/> 
    <s:TextInput id="ID" left="463" top="32" width="167" height="84"/> 

    <s:Button left="10" top="151" width="121" height="54" label="Save" click="Save(event)"/> 
    <s:Button left="139" top="151" width="146" height="54" label="Delete" click="Delete(event)" enabled="{listBox.selectedIndex != -1}"/> 
    <s:Button left="293" top="151" width="147" height="54" label="Update" click="Update(event)"/> 

    <s:List id="listBox" x="-1" y="265" width="641" height="167" itemRenderer="UserRenderer"></s:List> 

    <s:TextArea id="log" x="0" bottom="4" width="100%" height="221"/> 
</s:View> 

オプション2(リスト内のselectedItemに基づいてバインドするとします):

<?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" 
     creationComplete="view1_creationCompleteHandler(event)" > 

    <fx:Script> 
     <![CDATA[ 


      import mx.collections.ArrayCollection; 
      import mx.events.FlexEvent; 

      private var db:File = File.userDirectory.resolvePath("users01.db"); 
      private var conn:SQLConnection; 
      private var Stmt:SQLStatement; 

      [Bindable]private var dp:ArrayCollection = new ArrayCollection(); 

      protected function view1_creationCompleteHandler(event:FlexEvent):void 
      { 
       conn = new SQLConnection(); 
       conn.addEventListener(SQLEvent.OPEN, openHandler); 
       conn.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       conn.openAsync(db); 
      } 
      private function openHandler(event:SQLEvent):void { 
       log.text += "Database opened successfully"; 
       conn.removeEventListener(SQLEvent.OPEN, openHandler); 
       Stmt = new SQLStatement(); 
       Stmt.sqlConnection = conn; 
       Stmt.text = "CREATE TABLE IF NOT EXISTS UserTable (" + 
        "userId INTEGER PRIMARY KEY AUTOINCREMENT," + 
        "userLevel," + 
        "firstName TEXT," + "lastName TEXT)"; 
       Stmt.addEventListener(SQLEvent.RESULT, createResult); 
       Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       Stmt.execute(); 
      } 
      private function createResult(event:SQLEvent):void { 
       log.text += "\nTable created"; 
       conn.removeEventListener(SQLEvent.RESULT, createResult); 
       showResult(); 
      } 
      private function errorHandler(event:SQLErrorEvent):void { 
       log.text += "\nError message: " + event.error.message; 
       log.text += "\nDetails: " + event.error.details; 
      } 
      private function insertResult(event:SQLEvent):void { 
       log.text += "\nInsert completed"; 
       showResult(); 
      } 
      private function showResult():void { 
       Stmt = new SQLStatement(); 
       Stmt.sqlConnection = conn; 
       Stmt.text = "SELECT * FROM UserTable"; 
       Stmt.addEventListener(SQLEvent.RESULT, selectResult); 
       Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       Stmt.execute(); 
      } 
      private function selectResult(event:SQLEvent):void { 
       log.text += "\nSelect completed"; 
       var result:SQLResult = Stmt.getResult(); 
       listBox.dataProvider=new ArrayCollection(result.data); 
       listBox.selectedIndex = 0; //I ADDED THIS 
      } 
      protected function Save(event:MouseEvent):void   { 
       Stmt = new SQLStatement(); 
       Stmt.sqlConnection = conn; 
       Stmt.text = "INSERT INTO UserTable (firstName, lastName, userLevel)" + 
        "VALUES (:firstName, :lastName, :userLevel)"; 
       Stmt.parameters[":firstName"] = firstName.text; 
       Stmt.parameters[":lastName"] = lastName.text; 
       Stmt.parameters[":userLevel"] = ID.text; 
       Stmt.addEventListener(SQLEvent.RESULT, insertResult); 
       Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       Stmt.execute(); 
      } 
      protected function Delete(event:MouseEvent):void { 

       Stmt = new SQLStatement(); 
       Stmt.sqlConnection = conn; 
       Stmt.text="DELETE FROM UserTable WHERE lastName = ?"; 
       Stmt.parameters[0] = listBox.selectedItem.lastName; 
       Stmt.addEventListener(SQLEvent.RESULT, insertResult); 
       Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       Stmt.execute(); 
       trace("record deleted"); 
      } 
      protected function Update(event:MouseEvent):void   { 
       Stmt = new SQLStatement(); 
       Stmt.sqlConnection = conn; 
       Stmt.text = "UPDATE UserTable SET " + 
        "firstName=:firstName, " + 
        "lastName = :lastName " + 
        "WHERE userLevel = :userLevel2"; 
       Stmt.parameters[":firstName"] = firstName.text; 
       Stmt.parameters[":lastName"] = lastName.text; 
       Stmt.parameters[":userLevel2"] = ID.text; 
       Stmt.addEventListener(SQLEvent.RESULT, insertResult); 
       Stmt.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
       Stmt.execute(); 
      } 
     ]]> 
    </fx:Script> 

    <!-- I ADDED STUFF HERE --> 
    <s:Label text="FirstName" top="8" left="5" /> 
    <s:TextInput id="firstName" left="-1" top="36" width="233" height="80" text="@{listBox.selectedItem.firstName}" /> 

    <s:Label text="LastName" top="0" left="248"/> 
    <s:TextInput id="lastName" left="231" top="32" width="224" height="84"/> 

    <s:Label text="ID" top="0" left="483"/> 
    <s:TextInput id="ID" left="463" top="32" width="167" height="84"/> 

    <s:Button left="10" top="151" width="121" height="54" label="Save" click="Save(event)"/> 
    <s:Button left="139" top="151" width="146" height="54" label="Delete" click="Delete(event)" enabled="{listBox.selectedIndex != -1}"/> 
    <s:Button left="293" top="151" width="147" height="54" label="Update" click="Update(event)"/> 

    <s:List id="listBox" x="-1" y="265" width="641" height="167" itemRenderer="UserRenderer"></s:List> 

    <s:TextArea id="log" x="0" bottom="4" width="100%" height="221"/> 
</s:View> 
+0

指定されたIndexError:インデックス '0'が範囲外です。私もgetItemAt(1)を使用してみますが、それと同じエラーです。 – jameslcs

+0

これは、dpがまだ設定されていないことを意味します。 getItemAt()は、アイテムを取得する正しい方法、selectResultハンドラにデバッグブレークポイントをドロップする、私は使用していないので、getItemAt(0)は、コレクション内に1つのアイテムがある場合、 SQL APIの方がはるかに多いですが、そこにevent.resultがあるはずです。私はあなたがそのメソッドに書いたコードのどこかにdp = event.resultを割り当てていると仮定していました。それが意味をなさないかどうか私に教えてください。 – shaunhusain

+0

私はSQLStatementに関するドキュメントを見て、 – shaunhusain

関連する問題