android ListView 过滤器 - 空格字符 [英] Filter for android ListView - Space Character

查看:15
本文介绍了android ListView 过滤器 - 空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 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 自动生成的方法存根}public void beforeTextChanged(CharSequence s, int start, int count,输入后) {//TODO 自动生成的方法存根}公共无效 afterTextChanged(可编辑的){//TODO 自动生成的方法存根myAdapter.getFilter().filter(s.toString());}};

解决方案

如果 SimpleAdapter 的过滤功能不能满足您的过滤要求,您将需要覆盖 SimpleAdapter 并实现您自己的过滤要求.

这需要大量代码,因为SimpleAdapter 中的过滤器使用了大量私有对象,您需要复制这些对象.ArrayAdapter

同样适用

幸运的是,其他人 (@uʍop ǝpısdn) 已经参与了该流程,编写了代码并将其提供给社区.

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

问候.

I want to filter my listView using an EditText box and using the adapter getFilter() function. It works just fine until I put a space character in the text box.

Edit: It's a SimpleAdapter not ArrayAdapter

If my list contains these words: {"Apple", "Banana", "Red Apple"} If I type "apple" it will return all the items containing the word apple in it (Apple and Red Apple). If I type "apple " it won't returning anything.

Any ideas? Here's the code:

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

searchBox.addTextChangedListener(filterTextWatcher);

and

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());
}
};

解决方案

If the filtering functionality from SimpleAdapter doesn't fulfill your filtering requirements, you will need to override the SimpleAdapter and implement your own filtering requirements.

This requires a lot of code, as Filter in SimpleAdapter is using a lot of private objects, that you will need to replicate. The same aplies for ArrayAdapter

Fortunatly, someone else (@uʍop ǝpısdn) already went to the process, wrote the code and made it available for the community.

You can find the link to is blog here including how to use examples, and the code in here.

Regards.

这篇关于android ListView 过滤器 - 空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆