flex 1061:调用可能未定义的方法findbyterm

时间:2013-11-20 19:57:59

标签: actionscript-3 sqlite flex search flash-builder

EDITED:正在使用sqlite数据库开发移动应用程序,但我没有成功完成搜索功能

当我在调试器上运行程序时,它给了我这个错误:

  

1061:通过a调用可能未定义的方法findByTerm   静态类型mx.collections的引用:ArrayCollection。

这是我的主视图:

  <?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="Notes">
<fx:Script>
    <![CDATA[
        import model.Words;
        import model.WordsDatabase;
    ]]>
</fx:Script>

<s:actionContent>
    <s:Button label="Button" />
    <s:Button label="Add" click="onAddButtonClicked(event)"/>
    <s:Button click="WordsDatabase.words().findbyTerm(searchKey.text)"/>

</s:actionContent>

<s:List dataProvider="{WordsDatabase.words()}" change="onWordsSelected(event)"
        left="0" right="0" top="0" bottom="0">
    <s:itemRenderer>
        <fx:Component>
            <s:IconItemRenderer labelField="term" />
        </fx:Component>
    </s:itemRenderer>
</s:List>
<s:TextInput id="searchKey" x="0" y="0" width="480"/>
    </s:View>

使用WordsDatabase.as

的2个方法的模型包
    public static function findbyTerm(searchKey:String):ArrayCollection
    {
        var sql:String = "SELECT * FROM words WHERE term LIKE ?";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.parameters[0] = '%' + searchKey + '%';
        stmt.execute();
        var result:Array = stmt.getResult().data;
        if (result)
        {
            var wordsList:ArrayCollection = new ArrayCollection();
            for (var i:int=0; i<result.length; i++)
            {
                wordsList.addItem(processRow(result[i]));   
            }
            return wordsList;
        }
        else
        {
            return null;
        }
    }


    public static function words():ArrayCollection
    {
        var wordsList:ArrayCollection = new ArrayCollection();

        var sql:String = "SELECT id, term, defin, term1, defin1 FROM words";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.execute();
        var sqlResult:SQLResult = stmt.getResult();
        if (sqlResult) {
            var result:Array = sqlResult.data;
            if (result) {
     for (var index:Number = 0; index < result.length; index++)                 {wordsList.addItem(processRow(result[index]));                 
                }
            }
        }
        return wordsList;
    }


    protected static function processRow(o:Object):Words
    {
        var words:Words = new Words();
        words.id = o.id;
        words.term = o.term == null ? "" : o.term;
        words.defin = o.defin == null ? "" : o.defin;
        words.term1 = o.term1 == null ? "" : o.term1;
        words.defin1 = o.defin1 == null ? "" : o.defin1;
        return words;
    }

    public static function openDatabase(file:File):void
    {
        var newDB:Boolean = true;
        if (file.exists)
            newDB = false;
        _sqlConnection = new SQLConnection();
        _sqlConnection.open(file);
        if (newDB)
        {
            createDatabase();
            populateDatabase();
        }
    }

    protected static function createDatabase():void
    {
        var sql:String = 
            "CREATE TABLE IF NOT EXISTS words ( "+
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "term VARCHAR(50), " +
            "defin VARCHAR(250), " +
            "term1 VARCHAR(50), " +
            "defin1 VARCHAR(200))";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.execute();         
    }

    protected static function populateDatabase():void
    {
        var file:File = File.applicationDirectory.resolvePath("assets/words.xml");
        if (!file.exists) return;
        var stream:FileStream = new FileStream();
        stream.open(file, FileMode.READ);
        var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));
        stream.close();
        for each (var n:XML in xml.words)
        {
            var words:Words = new Words();
            words.id = n.id;
            words.term = n.term;
            words.defin = n.defin;
            words.term1 = n.term1;
            words.defin1 = n.defin1;
            addWords(words);
        }
    }

}
    }

1 个答案:

答案 0 :(得分:1)

您是否尝试从“Words”访问方法“findbyTerm”,这是一个ArrayCollection。

WordsDatabase.words()//返回一个ArrayCollection

| ArrayCollection | .findbyTerm(searchKey.text)//错误 WordsDatabase.words()。findbyTerm(searchKey.text)//错误

您应该更改搜索方式