Make filter more flexible
Now allows words to match out of order and restores searching in album and artist fields
This commit is contained in:
parent
775170c1bc
commit
d66f5dae08
@ -73,7 +73,22 @@ public class SongAdapter extends BaseAdapter implements Filterable {
|
|||||||
return mFilter;
|
return mFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String[] mRanges = { "[2abc]", "[3def]", "[4ghi]", "[5jkl]", "[6mno]", "[7pqrs]", "[8tuv]", "[9wxyz]"};
|
private static final String[] mRanges = { ".", "[2abc]", "[3def]", "[4ghi]", "[5jkl]", "[6mno]", "[7pqrs]", "[8tuv]", "[9wxyz]"};
|
||||||
|
private static Matcher createMatcher(String input)
|
||||||
|
{
|
||||||
|
String patternString = "";
|
||||||
|
for (int i = 0, end = input.length(); i != end; ++i) {
|
||||||
|
char c = input.charAt(i);
|
||||||
|
int value = c - '1';
|
||||||
|
if (value >= 0 && value < 9)
|
||||||
|
patternString += mRanges[value];
|
||||||
|
else
|
||||||
|
patternString += c;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Pattern.compile(patternString, Pattern.CASE_INSENSITIVE).matcher("");
|
||||||
|
}
|
||||||
|
|
||||||
private class ArrayFilter extends Filter {
|
private class ArrayFilter extends Filter {
|
||||||
@Override
|
@Override
|
||||||
protected FilterResults performFiltering(CharSequence filter)
|
protected FilterResults performFiltering(CharSequence filter)
|
||||||
@ -84,29 +99,30 @@ public class SongAdapter extends BaseAdapter implements Filterable {
|
|||||||
results.values = Arrays.asList(mAllObjects);
|
results.values = Arrays.asList(mAllObjects);
|
||||||
results.count = mAllObjects.length;
|
results.count = mAllObjects.length;
|
||||||
} else {
|
} else {
|
||||||
String patternString = "";
|
String[] words = filter.toString().split("\\s+");
|
||||||
for (int i = 0, end = filter.length(); i != end; ++i) {
|
Matcher[] matchers = new Matcher[words.length];
|
||||||
char c = filter.charAt(i);
|
for (int i = words.length; --i != -1; )
|
||||||
int value = c - '2';
|
matchers[i] = createMatcher(words[i]);
|
||||||
if (value >= 0 && value < 8)
|
|
||||||
patternString += mRanges[value];
|
|
||||||
else
|
|
||||||
patternString += c;
|
|
||||||
}
|
|
||||||
|
|
||||||
Pattern pattern = Pattern.compile(patternString);
|
|
||||||
Matcher matcher = pattern.matcher("");
|
|
||||||
|
|
||||||
int count = mAllObjects.length;
|
int count = mAllObjects.length;
|
||||||
ArrayList<Song> newValues = new ArrayList<Song>();
|
ArrayList<Song> newValues = new ArrayList<Song>();
|
||||||
newValues.ensureCapacity(count);
|
newValues.ensureCapacity(count);
|
||||||
|
|
||||||
|
outer:
|
||||||
for (int i = 0; i != count; ++i) {
|
for (int i = 0; i != count; ++i) {
|
||||||
Song value = mAllObjects[i];
|
Song song = mAllObjects[i];
|
||||||
matcher.reset(value.title.toLowerCase());
|
|
||||||
|
|
||||||
if (matcher.find())
|
for (int j = matchers.length; --j != -1; ) {
|
||||||
newValues.add(value);
|
if (song.artist != null && matchers[j].reset(song.artist).find())
|
||||||
|
continue;
|
||||||
|
if (song.album != null && matchers[j].reset(song.album).find())
|
||||||
|
continue;
|
||||||
|
if (song.title != null && matchers[j].reset(song.title).find())
|
||||||
|
continue;
|
||||||
|
continue outer;
|
||||||
|
}
|
||||||
|
|
||||||
|
newValues.add(song);
|
||||||
}
|
}
|
||||||
|
|
||||||
newValues.trimToSize();
|
newValues.trimToSize();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user