Skip to content

Instantly share code, notes, and snippets.

@samma835
Created November 14, 2012 09:25
Show Gist options
  • Save samma835/4071174 to your computer and use it in GitHub Desktop.
Save samma835/4071174 to your computer and use it in GitHub Desktop.
文本框中的搜索建议
@Override
public boolean onQueryTextChange(String searchText) {
// 清空上一次的搜索结果
GlobalInfo.searchGameResults.clear();
// 取得搜索结果集
int resultCount = 0;
for (HashMap<String, String> item : GlobalInfo.searchGameSet) {
if (item.get("game_name").contains(searchText)) {
resultCount++;
GlobalInfo.searchGameResults.add(item);
}
}
String[] results = new String[resultCount];
for (int i = 0; i < results.length; i++) {
results[i] = GlobalInfo.searchGameResults.get(i).get(
"game_name");
}
// 构造结果cursor
String[] columnNames = { "_id", "game_name" };
MatrixCursor cursor = new MatrixCursor(columnNames);
String[] template = new String[2];
int id = 0;
for (String item : results) {
template[0] = String.valueOf(id++);
template[1] = item;
cursor.addRow(template);
}
// 更新adapter
searchView.setSuggestionsAdapter(new SimpleCursorAdapter(
GlobalInfo.currentActivity, R.layout.search_result,
cursor, new String[] { "game_name" },
new int[] { R.id.game_name }));
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment