简体   繁体   English

如何在片段中使用getFilter()过滤ListAdapter

[英]How to Filter ListAdapter using getFilter() within a fragment

I want to filter a ListAdapter with a search action bar, that's a SimpleAdapter. 我想用搜索操作栏过滤ListAdapter,这是一个SimpleAdapter。 I'm struggling to understand how I can use getFilter within my onQueryTextSubmit & onQueryTextChange methods. 我正在努力了解如何在onQueryTextSubmit和onQueryTextChange方法中使用getFilter。 I'm using fragments. 我正在使用片段。

My onQueryTextSubmit & onQueryTextChange methods are working fine. 我的onQueryTextSubmit和onQueryTextChange方法工作正常。

Here is my onCreateOptionsMenu: 这是我的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);

}

Here is my ListAdapter: 这是我的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 }) {

Make the adapter as a global variable defined as 使适配器成为定义为的全局变量

ListAdapter listAdapter;

Before implementing the searchView in your application make sure that the adapter is showing the appropriate results, if so then implement the search 在您的应用程序中实现searchView之前,请确保适配器显示适当的结果,如果是,则实现搜索

@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM