I am trying to optimize the filter method for RecyclerView Adapter
in Android. The list is used as an ArrayList
. I have seen this post but they are filtering from the original list every time. They are not using old results if a character is added.
Example: if there are 10 results for String 'a' then user type 'm' , 'am' results are subset of 'a' results.
Please suggest code optimizations for this method and any test-case where it fails.
public void filter(String s) {
if (TextUtils.isEmpty(s)) {
mList.clear();
mList.addAll(copyList);
notifyItemRangeRemoved(0, getItemCount());
notifyItemRangeInserted(0, mList.size());
} else {
boolean isBackDirection = false;
List<WeekWorkBean> tempList = null;
if (s.length() < oldFilter.length()) {
tempList = new ArrayList<>();
tempList.addAll(mList);
mList.clear();
mList.addAll(copyList);
isBackDirection = true;
}
for (int i = 0; i < mList.size(); i++) {
WeekWorkBean bean = mList.get(i);
if (!bean.getEmpName().toLowerCase().startsWith(s.toLowerCase())) {
mList.remove(i);
if (!isBackDirection) {
notifyItemRemoved(i);
}
i--;
} else if (isBackDirection) {
if (tempList.size() != 0) {
for (WeekWorkBean b : tempList) {
if (!bean.getEmpId().equals(b.getEmpId())) {
notifyItemInserted(i);
}
}
} else {
notifyItemInserted(i);
}
}
}
oldFilter = s;
}
}