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

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

问题描述

我要筛选我的的ListView 使用的EditText 框,并使用适配器用getFilter ()功能。它工作得很好,直到我把一个空格字符在文本框中。

编辑:这是一个 SimpleAdapter不ArrayAdapter

如果我的列表中包含这些词:{苹果,香蕉,红苹果} 如果我输入苹果将返回所有包含在其中的单词apple(苹果和红苹果)的项目。 如果我输入苹果它不会返回任何东西。

任何想法?这里的code:

 搜索框=(EditText上)findViewById(R.id.searchBox);

sea​​rchBox.addTextChangedListener(filterTextWatcher);
 

 私人TextWatcher filterTextWatcher =新TextWatcher(){

公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
    // TODO自动生成方法存根
}

公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
            之后INT){
    // TODO自动生成方法存根
}

公共无效afterTextChanged(编辑S){
    // TODO自动生成方法存根
    。myAdapter.getFilter()滤波器(s.toString());
}
};
 

解决方案

如果从SimpleAdapter过滤功能不能满足您的滤波要求,你需要重写SimpleAdapter和实现自己的滤波要求。

这需要大量的code,如 SimpleAdapter 过滤器是使用大量的私人对象,你将需要复制。同样aplies为 ArrayAdapter

Fortunatly,别人(@uʍopǝpısdn)已经去的过程中,写了code,并使其可用于社区。

您可以找到链接,是博客这里,包括如何使用的例子,和code在<一个href="https://github.com/slezica/blogActivity/blob/master/src/com/slezica/tools/widget/FilterableAdapter.java"相对=nofollow>这里

问候。

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天全站免登陆