我有一个有两个输入的活动:一个用于城市,另一个用于位置。每当用户请求搜索操作时,我都会将城市和位置信息存储在 sqlite 表中。
现在,我希望每当用户输入城市(这是一个自动建议)时,位置中的自动建议选项应该自动更新。位置和城市的输入小部件都是 AutoCompleteTextView。
如何才能做到这一点?
Rgds,萨潘
我有一个有两个输入的活动:一个用于城市,另一个用于位置。每当用户请求搜索操作时,我都会将城市和位置信息存储在 sqlite 表中。
现在,我希望每当用户输入城市(这是一个自动建议)时,位置中的自动建议选项应该自动更新。位置和城市的输入小部件都是 AutoCompleteTextView。
如何才能做到这一点?
Rgds,萨潘
您只需要为 AutoCompleteTextView 提供一个 CursorAdapter,例如 SimpleCursorAdapter(尽管我尝试时 SimpleCursorAdapter 有问题)。
Cursor cursor = getCityCursor();
int ids = int[]{R.id.autocomplete_list_item};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.list_item, cursor, new String[]{DB.COL_CITIES});
AutoCompleteTextView actvCities = (AutoCompleteTextView)findViewById(R.id.cities);
actvCitites.setAdapter(cursorAdapter);
该getCityCursor();方法应提供由 a 获得的游标SQLiteDatabase.query()。
更新:我之前提到的“buggy” SimpleCursorAdapter 不是。只需提供一个CursorToStringConverter. dan breslau在这里写了一篇关于 AutoCompleteTextView 和 SimpleCursorAdapter 的非常好的文章。