过滤Android ListView - 空间角色

Chr*_*911 5 java android listview filter adapter

我想过滤我listView使用一个EditText盒子并使用适配器getFilter()功能.它工作得很好,直到我在文本框中放置一个空格字符.

编辑:这是一个SimpleAdapter而不是ArrayAdapter

如果我的列表中包含以下单词:{"Apple","Banana","Red Apple"}如果我输入"apple",它将返回包含苹果字样的所有项目(Apple和Red Apple).如果我输入"apple",它将不会返回任何内容.

有任何想法吗?这是代码:

searchBox = (EditText) findViewById(R.id.searchBox);

searchBox.addTextChangedListener(filterTextWatcher);
Run Code Online (Sandbox Code Playgroud)

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());
}
};
Run Code Online (Sandbox Code Playgroud)

Lui*_*uis 2

如果 SimpleAdapter 的过滤功能不能满足您的过滤要求,您将需要重写 SimpleAdapter 并实现您自己的过滤要求。

\n\n

这需要大量代码,因为 Filter inSimpleAdapter使用大量私有对象,您需要复制这些代码。这同样适用于ArrayAdapter

\n\n

幸运的是,其他人(@u\xca\x8dop \xc7\x9dp\xc4\xb1sdn)已经参与了该流程,编写了代码并将其提供给社区。

\n\n

您可以在此处找到博客的链接,包括如何使用示例以及此处的代码。

\n\n

问候。

\n