0

I am using a custom filter for a search function on my list of user installed applications. It works great but the search only works for search the application package names. In other words, it only searches the package name and not the actual application name. For example, to search for gmail, if I type in "gmail", I get no results, whereas if I search "gm" I get gmail in the results because it is a part of gmail's package name.

I want to search both the package name AND application name though, so how could I do this? Here is my coding:

    @Override
public Filter getFilter() {
    if(filter == null) {
        filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                List<ApplicationInfo> myFilteredAppList = new ArrayList<ApplicationInfo>();
                constraint = constraint.toString().toLowerCase();

                if (constraint.length() == 0) {
                    myFilteredAppList.addAll(originalListAppInfo);

                }

                for (ApplicationInfo appInfo : originalListAppInfo) {
                    String somefield = appInfo.packageName;
                    if (somefield.toLowerCase().contains(constraint.toString())) {
                        myFilteredAppList.add(appInfo);
                    }
                }
                results.count = myFilteredAppList.size();
                results.values = myFilteredAppList;
                return results;
            }


            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                if(results.values != null)
                {
                mListAppInfo = (List<ApplicationInfo>)results.values;
                notifyDataSetChanged();
                }
            }
        };
    }
    return filter;
}

I filter the search in this line:

String somefield = appInfo.packageName;

as you can see it filters just the packageName not the packageName and ApplicationName.

ADDED ERROR:

enter image description here

4

1 回答 1

0

尝试这个。

for (ApplicationInfo appInfo : originalListAppInfo) {
       String somefield = appInfo.packageName;
       String name = appInfo.name;

          if (somefield.toLowerCase().contains(constraint.toString().toLowerCase().toString())
                ||name.toLowerCase().contains(constraint.toString().toLowerCase().toString()) {
                  myFilteredAppList.add(appInfo);
          }
    }
于 2013-10-20T19:25:26.360 回答