以前我有EditText
onTextChangedListener
来处理用户搜索查询。这是通过这种方式完成的:
ed.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
textLength = ed.getText().length();
list.clear();
isPresent = initIsPresentArray();
for (int i = 0; i < tempList.length; i++) {
for (int j = 0; j < (tempList[i].length() - (textLength - 1)); j++) {
if (textLength <= tempList[i].length()) {
if (isPresent[i] == false) {
if (ed.getText()
.toString()
.equalsIgnoreCase(
(String) tempList[i]
.subSequence(
j,
(j + textLength)))) {
list.add(tempCaseList.get(i));
isPresent[i] = true;
}
}
}
}
if (list.size() == 0) {
error.setText(getResources().getString(R.string.notFound));
v.setVisibility(View.GONE);
} else {
error.setText("");
v.setVisibility(View.VISIBLE);
}
lv.setAdapter(new CustomAdapter(History.this, list));
}
}
});
这段令人讨厌的代码会动态更新ListView,同时用户输入要搜索的内容。它与EditText
相当不错。现在,我正在为我的项目实现ActionBarSherlock,并希望使用SearchWidget:
我夸大了我的菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.actionbarmenu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView)menu.findItem(R.id.searchCases).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setSubmitButtonEnabled(true);
return true;
}
如何将OnTextChangedListener
的相同行为应用于此SearchView
?可能吗?