gyo*_*dor 4 java sqlite android autocomplete android-cursoradapter
我有一个SQLite数据库,包含2个表4000+行,每行用于自动完成.我看到了非常简单的示例,它们使用字符串数组来提供自动完成功能,或者使用联系人列表来执行相同操作.显然,在我的案例中,这些都不起作用.对于自动完成,如何将自己的SQLite数据库与我自己的自动完成数据一起使用.我是否必须创建内容提供商?怎么样?请给我一些例子,因为我找不到任何例子.我已设法覆盖SQLiteOpenHelper
将数据库从assets文件夹复制到android上的/ data/data/MY_PACKAGE/databases /文件夹.我创建了一个CursorAdapter
使用我的自定义SQLiteOpenHelper
并从中返回游标的自定义runQueryOnBackgroundThread
.关于某些_id列丢失,我得到了奇怪的错误.我已将_id列添加到表中.我也不明白Filterable接口在做什么以及何时过滤我的数据.我需要覆盖哪些方法/类?谢谢.
有用.
你需要从这里获得SQLiteOpenHelper .您基本上必须将数据库从资产文件夹复制到特定文件夹中.然后,您需要一个使用自定义SQLiteOpenHelper的自定义CursorAdapter.
这是我的活动的onCreate方法.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
KeywordsCursorAdapter kwadapter = new KeywordsCursorAdapter(this, null);
txtKeyword = (AutoCompleteTextView)this.findViewById(R.id.txtKeyword);
txtKeyword.setAdapter(kwadapter);
txtCity = (AutoCompleteTextView)this.findViewById(R.id.txtCity);
btnSearch = (Button)this.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(this);
}
Run Code Online (Sandbox Code Playgroud)
这是cursoradapter.您可以在构造时为游标传递null.
public class KeywordsCursorAdapter extends CursorAdapter {
private Context context;
public KeywordsCursorAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
}
//I store the autocomplete text view in a layout xml.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.keyword_autocomplete, null);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
String keyword = cursor.getString(cursor.getColumnIndex("keyword"));
TextView tv = (TextView)view.findViewById(R.id.txtAutocomplete);
tv.setText(keyword);
}
//you need to override this to return the string value when
//selecting an item from the autocomplete suggestions
//just do cursor.getstring(whatevercolumn);
@Override
public CharSequence convertToString(Cursor cursor) {
//return super.convertToString(cursor);
String value = "";
switch (type) {
case Keywords:
value = cursor.getString(DatabaseHelper.KEYWORD_COLUMN);
break;
case Cities:
value = cursor.getString(DatabaseHelper.CITY_COLUMN);
break;
}
return value;
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
//return super.runQueryOnBackgroundThread(constraint);
String filter = "";
if (constraint == null) filter = "";
else
filter = constraint.toString();
//I have 2 DB-s and the one I use depends on user preference
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
//String selectedCountryCode = prefs.getString("selectedCountry", "GB");
String selectedCountryCode = prefs.getString(context.getString(R.string.settings_selected_country), "GB");
selectedCountryCode += "";
//Here i have a static SQLiteOpenHelper instance that returns a cursor.
Cursor cursor = MyApplication.getDbHelpers().get(selectedCountryCode.toLowerCase()).getKeywordsCursor(filter);
return cursor;
}
}
Run Code Online (Sandbox Code Playgroud)
这是返回光标的部分:它只是一个具有相似条件的选择.
public class DatabaseHelper extends SQLiteOpenHelper {
...
public synchronized Cursor getKeywordsCursor (String prefix) {
if (database == null) database = this.getReadableDatabase();
String[] columns = {"_id", "keyword"};
String[] args = {prefix};
Cursor cursor;
cursor = database.query("keywords", columns, "keyword like '' || ? || '%'", args, null, null, "keyword", "40");
int idcol = cursor.getColumnIndexOrThrow("_id");
int kwcol = cursor.getColumnIndexOrThrow("keyword");
while(cursor.moveToNext()) {
int id = cursor.getInt(idcol);
String kw = cursor.getString(kwcol);
Log.i("keyword", kw);
}
cursor.moveToPosition(-1);
return cursor;
}
...
}
Run Code Online (Sandbox Code Playgroud)
您还可以创建自定义内容提供程序,但在这种情况下,它只是您需要覆盖的另一个无用的类.