ArrayAdapter - filtering with multiple search terms

后端 未结 5 1720
臣服心动
臣服心动 2020-12-15 13:00

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 Arra

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 13:36

    Sam has already given an excellent answer But there is one very short problem For example, if I wanted to search for "a thing", I might enter "thing a". This will not work. However, the code above contains a short section which tries to return the words 'in order'. And you need to make some changes in it to get the best solution.

     // 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"
            for (int j = 1; j < prefixCount && loc > -1; j++) 
                loc = valueText.indexOf(prefixes[j]);
    
            // If every "word" is in this row, add it to the results
            if(loc > -1) 
                newValues.add(value);
        }
    

    I have removed loc + 2 from Sam's answer this line

    for (int j = 1; j < prefixCount && loc > -1; j++)
            loc = valueText.indexOf(' ' + prefixes[j], loc + 2);
    

提交回复
热议问题