【发布时间】:2012-03-13 12:38:11
【问题描述】:
我想使用EditText 框和适配器getFilter() 函数过滤我的listView。它工作得很好,直到我在文本框中输入一个空格字符。
编辑:这是一个 SimpleAdapter 而不是 ArrayAdapter
如果我的列表包含这些词:{“Apple”、“Banana”、“Red Apple”} 如果我输入“apple”,它将返回其中包含单词 apple 的所有项目(Apple 和 Red Apple)。 如果我输入“apple”,它不会返回任何东西。
有什么想法吗?代码如下:
searchBox = (EditText) findViewById(R.id.searchBox);
searchBox.addTextChangedListener(filterTextWatcher);
和
private TextWatcher filterTextWatcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
myAdapter.getFilter().filter(s.toString());
}
};
【问题讨论】:
-
你有想过这个吗?
-
我没有抱歉。我认为解决方案可能是基于 ArrayAdapter 制作自己的适配器,但我还没有尝试过(停止从事该项目)。
-
@CaptainProg 您在寻找什么类型的答案?使用
trim()提出的原始问题是最佳答案。 -
@Chris911 我也有类似的问题,我发了一个问题stackoverflow.com/questions/39573432/…希望你能帮忙。
标签: java android listview filter adapter