I am trying to get anytihng from my SQLite to render in Flex, and I can't figure out how to display any data, even text, in Flex. What am I doing wrong?
<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:ArrayCollecti开发者_如何学Goon;
[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>
I've tried a number of different components, and I can't seem to get the data to bind properly (although this code finally doesn't have the dreaded "Data binding will not be able to detect assignments to... " message, or the other dreaded "Property undefined" message).
Please help!
Fixed.
<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" />
I also recommend you read up more on how Flex works with lists and item renderers. There was a lot of problems with this code (ie. result.data.Index;
is wrong since data
is an array). From what I can gather, you'll want a custom item renderer. Furthermore, I don't recommend you use Binding if you don't need it (as with this example altogether) since it incurs extra resources.
精彩评论