0
我在實現searchview時顯示其服務器數據結果時遇到問題。我目前使用一個AsyncTask,所以我不會阻止用戶界面。這裏是我是如何實現它:SearchView AsyncTask UI未正確更新
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String query) {
if (query.length() > 0) {
new GetSearchByNameResultTask(searchView).execute(query);
} else {
return false;
}
return true;
}
@Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
return false;
}
});
GetSearchByNameResultTask
class GetSearchByNameResultTask extends AsyncTask<String, Void, String> {
private SearchView searchView;
public GetSearchByNameResultTask(SearchView searchView) {
this.searchView = searchView;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return WebRequestHelper.getInfo(params[0]);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
AppLog.d(TAG, "Result search: " + result);
Gson gson = new Gson();
searchResponseList = gson.fromJson(result, new TypeToken<List<Info>>() {
}.getType());
loadHistory(searchView, searchResponseList);
// TODO: search adapter
}
}
loadHistory()代碼:
private void loadHistory(SearchView searchView, List<AppInfo> searchResponse) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
searchView.setSuggestionsAdapter(null);
// Cursor
String[] columns = new String[] { "_id", "text" };
Object[] temp = new Object[] { 0, "default" };
MatrixCursor cursor = new MatrixCursor(columns);
final List<Info> items = searchResponse;
for (int i = 0; i < items.size(); i++) {
temp[0] = i;
temp[1] = items.get(i).getName();
cursor.addRow(temp);
}
CursorAdapter ca = new AppListSearchAdapter(this, cursor, items);
new AsyncQueryResult(cursor,ca).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
// SearchView
searchView.refreshDrawableState();
searchView.setSuggestionsAdapter(ca);
searchView.setOnSuggestionListener(new OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int position) {
// TODO Auto-generated method stub
return false;
}
});
}
}
問題 一切運行正常的。我收到搜索結果。但我在更新視圖時遇到問題。用一個例子也許最好的解釋:
數據:在搜索
Hell Hello Helicopter
文本:赫爾
結果:地獄
:在搜索Hello World Hello Helicopter
文本
Hell Hello [extra empty space]
額外的空白空間是我的問題。 「列表視圖」不會更新以適應結果,直到我輸入'你好'