ArrayAdapter - 过滤与多个搜索字词 [英] ArrayAdapter - filtering with multiple search terms

查看:142
本文介绍了ArrayAdapter - 过滤与多个搜索字词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近增加了一个赏金<一个href="http://stackoverflow.com/questions/9407428/filter-for-android-listview-space-character/13354555#13354555">this SO质疑,但实现了原来的问题要求一个SimpleAdapter,而不是一个ArrayAdapter。所以,这个问题涉及到一个ArrayAdapter:

I have recently added a bounty to this SO question, but realise the original question asks for a SimpleAdapter and not an ArrayAdapter. So, this question relates to the ArrayAdapter:

我想能够使用多个搜索词一个ListView过滤的ArrayAdapter。例如,如果我的列表包含以下项目:

I would like to be able to filter an ArrayAdapter in a ListView using multiple search terms. For example, if my list contains the following items:

a thing
another thing
a different thing
nothing
some thing

如果我搜索的事情',那么的所有的条款应在过滤器中返回结果。这是一个可以用下面的code来实现正常的行为:

If I search for 'thing' then all the terms should be returned in the filter results. This is normal behaviour that can be accomplished using the following code:

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

不过,如果我输入的搜索短语的事',我希望下面的结果显示:

However; if I enter the search phrase 'a thing', I expect the following results to be shown:

a thing
another thing
a different thing

其实,我得到的结果中的的事'返回。由于过滤器是寻找整个字符串,它将该空间作为检索词的一部分。从本质上讲,我问的是我如何过滤列表不是由一个特定字符串,而是通过多个搜索词,每一个用空格隔开?只要每个搜索条件中的项目出现至少一次,则该项目应在结果中返回。

In fact, all I get returned in the result 'a thing'. Since the filter is looking for the entire string, it treats the space as a part of the search term. Essentially, what I'm asking is how do I filter the list not by one specific string, but by multiple search terms, each separated by spaces? So long as each of the search terms appear at least once in the item, then that item should be returned in the result.

类似的问题似乎已经问过的SO(例如,看到这个帖子,它涉及SimpleAdapters顶部的链接),但远不具备,我发现做到这一点实际的方式。我想答案会涉及对每个字符串多次分离搜索短语成单独的字符串,由空格字符分隔,然后运行搜索,并且只返回相匹配的迭代的所有的结果......

Similar questions seem to have been asked before on SO (for example, see the link at the top of this post, which concerns SimpleAdapters), but nowhere have I found an actual way to accomplish this. I suppose the answer would involve separating the search phrase into individual strings, delimited by the space character, and then running the search multiple times on each string, and only returning the results which match for all of the iterations...

推荐答案

不幸的是ArrayAdapter和其他内置适配器不允许你改变其现有的过滤器。还有就是API中没有使用setFilter()方法......你必须创建一个执行相同的功能作为ArrayAdapter定制的适配器。

Unfortunately ArrayAdapter and other built-in adapters don't allow you to change their existing filters. There is no setFilter() method in the API... You must create a custom adapter that performs the same functions as ArrayAdapter.

要做到这一点:简单地剪切和放大器;粘贴<一href="http://grep$c$c.com/file_/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/widget/ArrayAdapter.java/?v=source">ArrayAdapter's来源$ C ​​$ C 到一个新的类项目。然后找到底部的过滤器。在 performFiltering(),我们会做一些修改。找到的if-else块标有以下注释,并替换code座这一点:

To do this: simply cut & paste ArrayAdapter's source code into a new class in your project. Then find the Filter at the bottom. Inside performFiltering() we'll make a few changes. Find the if-else block marked with the following comment and replace that code block with this:

// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString)) {
    newValues.add(value);
} else {
    // Break the prefix into "words"
    final String[] prefixes = prefixString.split(" ");
    final int prefixCount = prefixes.length;

    int loc;
    // Find the first "word" in prefix
    if(valueText.startsWith(prefixes[0]) || (loc = valueText.indexOf(' ' + prefixes[0])) > -1)
        loc = valueText.indexOf(prefixes[0]);

    // Find the following "words" in order
    for (int j = 1; j < prefixCount && loc > -1; j++) 
        loc = valueText.indexOf(' ' + prefixes[j], loc + 2);

    // If every "word" is in this row, add it to the results
    if(loc > -1) 
        newValues.add(value);
}

就是这样。我们只有改变code段以上,以满足您的要求,但我们必须在整个适配器复制到一类,因为那里是为了使这些变化没有别的办法。

That's it. We only had to change the section of code above to meet your requirements, but we have to copy the entire adapter into a class since there is no other way to makes these changes.

(PS既然你已经创建了一个新的类,你还不如优化 getView(),以满足您的需要。通用的​​方法是比它慢需要是自定义类。)

(PS Since you've already created a new class, you might as well optimize getView() to suit you needs. The generic method is slower than it needs to be for a custom class.)

这篇关于ArrayAdapter - 过滤与多个搜索字词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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