如何将ArrayAdapter<String>
静态数据转换CursorAdapter
为使用 Suggestion Listener 的SearchView
?我已经ArrayAdapter<String>
从静态数据 ( allString
)
ArrayAdapter<String> searchAdapter = new ArrayAdapter<String>(context, R.layout.listitem, allString);
我将它用于MultiAutoCompleteTextView
API 级别低于 11 的设备上运行良好
MultiAutoCompleteTextView findTextView.setAdapter(searchAdapter);
但是,我的目标 API 级别为 11,对于 API>10,我使用ActionBar
我希望在其中使用 SearchView 的一个。
这是我尝试过的:它确实显示ActionBar
了嵌入的SearchView
,但没有像在MultiAutoCompleteTextView
.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
if (android.os.Build.VERSION.SDK_INT > 10){
inflater.inflate(R.menu.menu11, menu);
searchView = (SearchView) menu.findItem(R.id.MENU_SEARCH).getActionView();
int[] to = {0};
CursorAdapter cursorAdapter = new SimpleCursorAdapter(context, R.layout.listitem, null, allBusStopString, to);
searchView.setSuggestionsAdapter(cursorAdapter);
searchView.setOnSuggestionListener(new OnSuggestionListener() {
@Override
public boolean onSuggestionClick(int position) {
String selectedItem = (String)cursorAdapter.getItem(position);
Log.v("search view", selectedItem);
return false;
}
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
});
}else{
inflater.inflate(R.menu.menu, menu);
}
return true;
}