搜索查看该过滤列表视图,自定义listview_items [英] Searchview that filters a listview, with custom listview_items

查看:141
本文介绍了搜索查看该过滤列表视图,自定义listview_items的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序已经得到了一个自定义的ListView适配器是一个膨胀listview_item,其中有多个TextViews成一个ListView。

My app has got a custom ListView-Adapter that inflates a listview_item, which has multiple TextViews into a ListView.

我已经加入一个搜索查看到布局,但我希望有它,以便它可以搜索ListView和通过查看是否在搜索查看键入的数据是相同的任何从listview_items所述TextViews数据的过滤。

I have added a SearchView to the layout but I want have it so that it can search the ListView and filter it by looking if the data typed in the SearchView is the same as any of the data in the TextViews from the listview_items.

推荐答案

覆盖OnQueryTextListener在活性的研究类,如:

Override OnQueryTextListener in your acitivity class like :

@Override
public boolean onQueryTextChange(String newText) {
    mAdapter.getFilter().filter(newText);
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    mAdapter.getFilter().filter(query);
    return true;
}

和适配器里面,实施像过滤器:

And inside adapter, implement filter like :

public Filter getFilter() {
    if (mFliter == null)
        mFliter = new CustomFilter();
    return mFliter;

}
    private class CustomFilter extends Filter {
        // called when adpater filter method is called
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if (constraint != null && constraint.toString().length() > 0) {
                ArrayList<CustomObject> filt = new ArrayList<CustomObject>(); //filtered list
                for (int i = 0; i < originalList.size(); i++) {
                    CustomObject m = originalList.get(i);
                    if (m.getName().toLowerCase().contains(constraint)) {
                        filt.add(m); //add only items which matches
                    }
                }
                result.count = filt.size();
                result.values = filt;
            } else { // return original list
                synchronized (this) { 
                    result.values = originalList;
                    result.count = originalList.size();
                }
            }
            return result;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            if (results != null) {
                setList((ArrayList<CustomObject>) results.values); // notify data set changed
            } else {
                setList((ArrayList<CarObject>) originalList);
            }
        }
    }

    public void setList(ArrayList<CarObject> data) {
        mList = data; // set the adapter list to data
        YourAdapter.this.notifyDataSetChanged(); // notify data set change
    }

这篇关于搜索查看该过滤列表视图,自定义listview_items的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆