Simplify ApplicationAdapter filter

This commit is contained in:
topjohnwu 2017-07-23 18:33:16 +08:00
parent 085fba050a
commit 76e7c5623d

View File

@ -129,30 +129,23 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
@Override @Override
protected FilterResults performFiltering(CharSequence constraint) { protected FilterResults performFiltering(CharSequence constraint) {
List<ApplicationInfo> filteredApps;
if (constraint == null || constraint.length() == 0) { if (constraint == null || constraint.length() == 0) {
filteredApps = mOriginalList; mList = mOriginalList;
} else { } else {
filteredApps = new ArrayList<>(); mList = new ArrayList<>();
String filter = constraint.toString().toLowerCase(); String filter = constraint.toString().toLowerCase();
for (ApplicationInfo info : mOriginalList) { for (ApplicationInfo info : mOriginalList) {
if (Utils.lowercaseContains(info.loadLabel(pm), filter) if (Utils.lowercaseContains(info.loadLabel(pm), filter)
|| Utils.lowercaseContains(info.packageName, filter)) { || Utils.lowercaseContains(info.packageName, filter)) {
filteredApps.add(info); mList.add(info);
} }
} }
} }
return null;
FilterResults results = new FilterResults();
results.values = filteredApps;
results.count = filteredApps.size();
return results;
} }
@SuppressWarnings("unchecked")
@Override @Override
protected void publishResults(CharSequence constraint, FilterResults results) { protected void publishResults(CharSequence constraint, FilterResults results) {
mList = (List<ApplicationInfo>) results.values;
notifyDataSetChanged(); notifyDataSetChanged();
} }
} }