我有一个我过滤的ListView和EditText,我有一个TextWatcher来获取文本更改事件。在您从编辑文本中删除文本之前,一切都很有效。然后过滤器保留为第一个字母,不会返回到过滤器。
我做错了什么?这是TextWatcher的代码,我甚至试图禁用过滤器但是没有效果。
EditText txtFilter = (EditText) this.findViewById(R.id.txtFilter);
txtFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length()==0)
{
lv.setTextFilterEnabled(false);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
lv.setTextFilterEnabled(true);
lv.setFilterText(s.toString());
}
});
感谢您的帮助
答案 0 :(得分:2)
我认为Adil Soomro的回答很糟糕......我只看看android源码和setFilterText看起来像
public void setFilterText(String filterText) {
// TODO: Should we check for acceptFilter()?
if (mTextFilterEnabled && !TextUtils.isEmpty(filterText)) {
createTextFilter(false);
// This is going to call our listener onTextChanged, but we might not
// be ready to bring up a window yet
mTextFilter.setText(filterText);
mTextFilter.setSelection(filterText.length());
if (mAdapter instanceof Filterable) {
// if mPopup is non-null, then onTextChanged will do the filtering
if (mPopup == null) {
Filter f = ((Filterable) mAdapter).getFilter();
f.filter(filterText);
}
// Set filtered to true so we will display the filter window when our main
// window is ready
mFiltered = true;
mDataSetObserver.clearSavedState();
}
}
}
所以你可以看到它用过滤器设置适配器......
您应该使用lv.clearTextFilter();
代替lv.setTextFilterEnabled(false);
答案 1 :(得分:1)
不是过滤ListView
,而是像这样过滤Adapter
:
txtFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count){
adapter.getFilter().filter(s);
}
});