问题描述
我想使用搜索操作栏(即SimpleAdapter)来过滤ListAdapter.我正在努力了解如何在onQueryTextSubmit&中使用getFilter. onQueryTextChange方法.我正在使用片段.
I want to filter a ListAdapter with a search action bar, that's a SimpleAdapter. I'm struggling to understand how I can use getFilter within my onQueryTextSubmit & onQueryTextChange methods. I'm using fragments.
我的onQueryTextSubmit& onQueryTextChange方法工作正常.
My onQueryTextSubmit & onQueryTextChange methods are working fine.
这是我的onCreateOptionsMenu:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu,inflater);
SearchManager searchManager = (SearchManager) this.getContext().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
// adapter.getFilter().filter(newText);
// adapter.getFilter().filter(newText);
System.out.println("on text chnge text: "+newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
// adapter.getFilter().filter(query);
// adapter.getFilter().filter(query);
System.out.println("on query submit: "+query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
// return super.onCreateOptionsMenu(menu);
}
这是我的ListAdapter:
ListAdapter adapter = new SimpleAdapter(getActivity(), inboxList, R.layout.p_list_item,
new String[] { TAG_ID, TAG_FROM, TAG_COUNTRY, TAG_DATE, TAG_EMAIL, TAG_SUBJECT },
new int[] { R.id.from, R.id.subject, R.id.country, R.id.date, R.id.mail, R.id.roundscore }) {
推荐答案
将适配器作为定义为
ListAdapter listAdapter;
在您的应用程序中实现searchView之前,请确保适配器显示适当的结果,如果是,则实现搜索
Before implementing the searchView in your application make sure that the adapter is showing the appropriate results, if so then implement the search
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu,menu);
MenuItem item = menu.findItem(R.id.search_menu);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
listAdapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
这篇关于如何在片段中使用getFilter()过滤ListAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!