关闭

android api demos 之 ListView的搜索功能

1341人阅读 评论(4) 收藏 举报






上面两个图片是设定画面,显示当前应用的画面,当输入一个“a”时,就会自动搜索出以“a”开头的正在运行的应用。这个功能的实现方法:

1.      使用SimpleAdapter

2.      设置setTextFilterEnabled(true)

setListAdapter(new SimpleAdapter(this, getData(path),

                android.R.layout.simple_list_item_1, new String[] { "title" },

                new int[] { android.R.id.text1 }));

getListView().setTextFilterEnabled(true);

其实SimpleAdapter实现了一个接口Filterable  ,只要我们自己写的Adapter也实现这个接口,我们的ListView也可以实现这个功能。

 

Filterable接口:

public interface Filterable{

    Filter getFilter();

}

Filter是一个抽象类,它定义以及实现了一个过滤规则。看SimpleAdapter中有Filter的实现。

private class SimpleFilter extends Filter {

 

        @Override

        protected FilterResults performFiltering(CharSequenceprefix) {

            FilterResults results = new FilterResults();

 

            if (mUnfilteredData == null) {

                mUnfilteredData = new ArrayList<Map<String, ?>>(mData);

            }

 

            if (prefix == null || prefix.length() == 0) {

                ArrayList<Map<String,?>> list = mUnfilteredData;

                results.values = list;

                results.count = list.size();

            } else {

                String prefixString =prefix.toString().toLowerCase();

 

                ArrayList<Map<String,?>> unfilteredValues = mUnfilteredData;

                int count = unfilteredValues.size();

 

                ArrayList<Map<String,?>> newValues = new ArrayList<Map<String, ?>>(count);

 

                for (int i = 0; i < count; i++) {

                    Map<String, ?> h =unfilteredValues.get(i);

                    if (h != null) {

                       

                        int len = mTo.length;

 

                        for (int j=0; j<len; j++){

                            String str =  (String)h.get(mFrom[j]);

                           

                            String[] words =str.split(" ");

                            int wordCount = words.length;

                           

                            for (int k = 0; k <wordCount; k++) {

                                String word =words[k];

                               

                                if(word.toLowerCase().startsWith(prefixString)) {

                                    newValues.add(h);

                                    break;

                                }

                            }

                        }

                    }

                }

 

                results.values = newValues;

                results.count =newValues.size();

            }

 

            return results;

        }

 

        @Override

        protected void publishResults(CharSequence constraint,FilterResults results) {

            //noinspection unchecked

            mData = (List<Map<String, ?>>)results.values;

            if (results.count > 0) {

                notifyDataSetChanged();

            } else {

                notifyDataSetInvalidated();

            }

        }

    }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5616次
    • 积分:96
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    文章存档
    最新评论