0
辞書検索で、人がテキストを入力すると、候補が飛び出します。 データベースは英語の辞書です。オートコンプリート検索速度を上げる方法
//Search System
final Context ctx = this;
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (autoCompleteTextView.getText().length() >= 2) {
DbHandler handler = new DbHandler(getApplicationContext());
final ArrayList<String> Deflist = handler.getSearchList(autoCompleteTextView.getText().toString().trim());
suggestions = Deflist;
handler.close();
if (Deflist != null) {
Search.this.runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayAdapter adapter = new ArrayAdapter(ctx, android.R.layout.simple_list_item_1, Deflist);
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setThreshold(1);
}
});
}
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
データベース・コード
public void indexDB()
{
try{
getWritableDatabase().execSQL("CREATE INDEX words_index ON entries(word)");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public ArrayList<String> getSearchList(String partWord) {
try {
if (partWord == "") {
return null;
}
ArrayList<Definition> DefList = new ArrayList<>();
ArrayList<String> test = new ArrayList<>();
Definition definition = null;
Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE word LIKE '" + partWord + "%'", null);
try {
if (cursor.moveToFirst()) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String word = cursor.getString(cursor.getColumnIndex("word"));
if (!test.contains(word)) {
test.add(word);
}
}
}
} catch (Exception e) {
cursor.close();
}
return test;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
私はそれは、(ドロップ検索を開始するために2以上に制限を設定しても、別のスレッドで実行しようとしたインデックスに試してみましたマルチスレッドの考え方の不一致)。それを本当に速くする方法はありますか?
の代わりに、あなたはアルファベット –
に基づいて短いテーブルをいけない理由を穴テーブルからserchingは、その辞書DBを@NeelaySrivastavaそのすでにソート。 –
あなたはこれをフォローしていますか? https://developer.android.com/training/search/search.html –