如何使用searchview搜索数据?

时间:2014-10-16 11:48:03

标签: android xml sqlite searchview

public Cursor searchByInputText(String inputText) throws SQLException {

    String query = "SELECT tag as _id," +
            TABLE_CONTACTS +  " from " + TABLE_CONTACTS +
            " where " + TABLE_CONTACTS + " MATCH '" + inputText + "';";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor Cursor = db.rawQuery(query,null);

    if (Cursor != null) {
        Cursor.moveToFirst();
    }
    return Cursor;

}

我尝试了上面的代码,但我无法搜索。

我正在尝试获取存储在我的数据库中的标记字段数据,一旦我在搜索框中搜索它没有显示我的数据库中的任何数据。

1 个答案:

答案 0 :(得分:0)

尝试使用SearchView的setOnQueryTextListener

类似的东西:

new SearchView.OnQueryTextListener( ) {
    @Override
    public boolean   onQueryTextChange( String newText ) {
        // this is called when text changes in SearchView 
        textView.setText(newText);
    }

    @Override
    public boolean   onQueryTextSubmit(String query) {
        // this is called when Search Button is clicked
        textView.setText(query);
    }
}