如何在Android中的自定义列表视图中创建搜索选项

时间:2016-10-08 15:29:15

标签: android listview android-toolbar android-search

看,我的Android Activity解析数据列表(名称,地址)并在listview上显示。 我在Activity中使用的是什么

 private List<RestListModel> rdataList= new ArrayList<RestListModel>();
  private ListView listView;
  private CustomListAdapter adapter;

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quick_list);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
        getSupportActionBar().setDisplayShowTitleEnabled(false);            

        listView = (ListView) findViewById(R.id.rrlist);
        adapter = new CustomListAdapter(this, rdataList);
        listView.setAdapter(adapter);
        listView.setTextFilterEnabled(true);
    }

////这是Activity

中的搜索选项代码
  public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menutosearch, menu);

        final MenuItem myActionMenuItem = menu.findItem(R.id.action_search);

        search = (SearchView) myActionMenuItem.getActionView();

        //*** setOnQueryTextListener ***
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                // TODO Auto-generated method stub    
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // TODO Auto-generated method stub
                adapter.filter(newText);
                return false;
            }
        });
        return true;
    }

//我的自定义列表适配器就是这样。

public class CustomListAdapter extends BaseAdapter {
        private Activity activity;
        private LayoutInflater inflater;
        private List<RestListModel> rItems;
        private ArrayList<RestListModel> arraylist;
        ImageLoader imageLoader = AppController.getInstance().getImageLoader();

        public CustomListAdapter(Activity activity, List<RestListModel> restItems) {
            this.activity = activity;
            this.rItems = restItems;
            this.arraylist = new ArrayList<RestListModel>();
            this.arraylist.addAll(restItems);
        }

///在CustomListAdapter中过滤方法代码

//过滤方法

public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            rItems.clear();
            if (charText.length() == 0) {
                rItems.addAll(arraylist);
            } else {
                for (RestListModel wp : arraylist) {
                    if (wp.getName().toLowerCase(Locale.getDefault())
                            .contains(charText)) {
                        rItems.add(wp);
                    }
                }
            }
            notifyDataSetChanged();
        }

0 个答案:

没有答案