我可以打开SearchView,但无论我尝试什么,我都无法恢复搜索内容......
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.menu_phone_contacts_list, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
if (DataManager.get().getLastContactFilter() != null)
{
MenuItemCompat.expandActionView(searchItem);
// Variant 1 - does not work:
searchView.setQuery(DataManager.get().getLastContactFilter(), false);
// Variant 2 - does not work either:
searchView.post(new Runnable() {
@Override
public void run() {
searchView.setQuery(DataManager.get().getLastContactFilter(), false);
}
});
}
变体3正在恢复以下功能中的内容:
@Override
public void onPrepareOptionsMenu (Menu menu)
{
super.onPrepareOptionsMenu(menu);
if (DataManager.get().getLastContactFilter() != null)
{
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setQuery(DataManager.get().getLastContactFilter(), false);
}
}
但没有变体确实有用......
答案 0 :(得分:0)
使用静态MenuItemCompat方法获取SearchView
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQuery("myQueryText", false);
并检查xml项目菜单,它需要" app"前缀(与平台无关)
app:actionViewClass="android.support.v7.widget.SearchView"