【发布时间】:2018-05-19 12:19:14
【问题描述】:
这是我的TextWatcher:
searchBox.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged( CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(final CharSequence charSequence, int i, int i1, int i2) {
ArrayList<ProductInformation> newList = new ArrayList<>();
for (ProductInformation info : currentList)
if (info.getName().contains(charSequence))
newList.add(info);
productListAdapter.setNewData(newList);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
将这个listener 添加到我的editText 后,打字时会出现延迟。
我猜问题是在列表中搜索(列表中有 40 到 60 个项目)。
我能做什么?
【问题讨论】:
-
对项目使用 TreeMap 或 HashMap 而不是 ArrayList。
-
@Juan 有什么区别?
-
要在ArrayList中查找,它有顺序一一检查,TreeMap使用二分查找,hashmap应该一步解决查找。 Hashmap 不完全是这样,但它就是这个想法。