0 Replies Latest reply on Jan 12, 2011 1:57 PM by Rob-2007

    ArrayCollection

    Rob-2007

      Hello guys,

      I being trying to populate a dropdown list from a database with no luck. Here is the code I'm working with.

       

      schedule.as

      import flash.data.SQLStatement;
      import flash.errors.SQLError;
      import flash.events.Event;
      import flash.events.SQLErrorEvent;
      import flash.events.SQLEvent;
      import flash.events.TimerEvent;
      import flash.filesystem.File;
      import flash.utils.Timer;
      
      import mx.collections.ArrayCollection;
      import mx.controls.Alert;
      import mx.utils.ObjectUtil;
      
      import org.osmf.events.TimeEvent;
      
      private var sqlc:SQLConnection = new SQLConnection();
      private var sqls:SQLStatement = new SQLStatement();
      
      [Bindable]
      private var schedule_dp:ArrayCollection = new ArrayCollection();
      
      private function start():void{
           var db:File = File.applicationStorageDirectory.resolvePath("softball.db");
           sqlc.openAsync(db);
           sqlc.addEventListener(SQLEvent.OPEN, db_opened);
           sqlc.addEventListener(SQLErrorEvent.ERROR, error);
           sqls.addEventListener(SQLErrorEvent.ERROR, error);
           sqls.addEventListener(SQLEvent.RESULT, result);
      }
      
      private function db_opened(e:SQLEvent):void{
           sqls.sqlConnection = sqlc;
           sqls.text = "CREATE TABLE IF NOT EXISTS schedule(scheduleId INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "hTeam INTEGER, vTeam INTEGER, gameDate TEXT, gameTime TEXT);";
           sqls.execute();
           getSchedule();
      }
      
      private function addSchedule():void{
           sqls.text = "INSERT INTO schedule(gameDate, gameTime, status) " +
                "VALUES('"+a_gameDate.text+"', '"+a_gameTime.selectedItem+"', '"+a_status.selectedItem+"');";
           sqls.execute();
           getSchedule();
      }
      
      private function getSchedule(e:TimerEvent = null):void{
           var timer:Timer = new Timer(10,1);
           timer.addEventListener(TimerEvent.TIMER, getSchedule);
           if ( !sqls.executing ){
                sqls.text = "SELECT t1.tNumber AS hNumber, t2.tNumber AS vNumber, t1.tName AS hTeam, t2.tName AS vTeam, " +
                     "sc.gameDate, sc.gameTime, sc.status " +
                     "FROM schedule sc " +
                     "INNER JOIN teams t1 ON sc.hTeam = t1.teamsId " +
                     "INNER JOIN teams t2 ON sc.vTeam = t2.teamsId " +
                     "ORDER BY gameDate ASC" //TODO - Status
                sqls.execute();
           }
           else{
                timer.start();
           }
      }
      
      private function deleteSchedule():void
      {
           sqls.text = "DELETE FROM schedule WHERE scheduleId="+schedule_dp[schedule_dg.selectedIndex].scheduleId;
           sqls.execute();
           getSchedule();
      }
      
      private function result(e:SQLEvent):void{
           var schedule:Array = sqls.getResult().data;
           schedule_dp = new ArrayCollection(schedule);
      }
      
      private function error(e:SQLErrorEvent):void{
           Alert.show(e.toString());
      }
      

       

      ScheduleView.mxml

      <fx:Script source="../valueObjects/_Schedule.as"/>
      
      <s:SkinnableContainer id="aForm" styleName="bgBox" y="245">
                <s:Group>
                     <s:layout>
                          <s:HorizontalLayout gap="5"/>
                     </s:layout>
                     <mx:DateField id="a_gameDate" yearNavigationEnabled="true" color="0x000000"/>
                     <s:DropDownList id="a_gameTime" width="70">
                          <s:dataProvider>
                               <s:ArrayList source="[09:00 AM, 10:00 AM, 11:00 AM]" />
                          </s:dataProvider>
                     </s:DropDownList>               
                     <s:DropDownList id="a_vTeam" width="50" dataProvider="{teams_dp}" labelField="tName" />                              
                     <s:DropDownList id="a_hTeam" width="50" dataProvider="{teams_dp}" labelField="tName" />
                     <s:DropDownList id="a_status" width="50">
                          <s:dataProvider>
                               <s:ArrayList source="[TBP, W, L, T, C]" />
                          </s:dataProvider>
                     </s:DropDownList>     
                     <s:Button id="a_submit" click="addSchedule()" x="60" y="10" label="H" width="40" height="20"/>
                </s:Group>
           </s:SkinnableContainer>
           
           <s:SkinnableContainer id="dg" height="100%" width="450" y="15">
                <s:Group>
                     <mx:DataGrid id="schedule_dg" dataProvider="{schedule_dp}" showHeaders="false" height="100%" width="100%">
                          <mx:columns>
                               <mx:DataGridColumn width="80" textAlign="left" dataField="gameDate"/>
                               <mx:DataGridColumn width="70" textAlign="left" dataField="gameTime"/>
                               <mx:DataGridColumn width="15" textAlign="left" dataField="vNumber"/>
                               <mx:DataGridColumn width="105" textAlign="left" dataField="vTeam"/>
                               <mx:DataGridColumn width="15" textAlign="left" dataField="hNumber"/>
                               <mx:DataGridColumn width="115" textAlign="left" dataField="hTeam"/>
                               <mx:DataGridColumn width="50" textAlign="left" dataField="status"/>
                          </mx:columns>
                     </mx:DataGrid>
                </s:Group>
           </s:SkinnableContainer>     
      

       

      Ok, everything works fine but I'm trying to populate two DropDownList from a different table and I can't. teams_dp does not exist yet.

      The table name is teams and I will use it to populate both  DropDownLists a_vTeam and a_hTeam to insert the id in the schedule tables.

       

      Please Help, thanks