ARTICLE AD BOX
I am making an android app, with Java, that uses the SearchableSpinner from "com.github.miteshpithadiya:SearchableSpinner:master"
I would like to modify it so it also can search for multiple words in the same string, in any order. For example, if I searched for the words "red grapefruit" in a list of foods, the Spinner would be reduced to these options:
"Grapefruit, pink or red, all areas, raw" "Grapefruit, pink or red, california or arizona, raw" "Grapefruit, pink or red, florida, raw"Here is the xml:
<com.toptoche.searchablespinnerlibrary.SearchableSpinner android:id="@+id/spinner_view" android:layout_width="match_parent" android:layout_height="50dp" app:layout_constraintTop_toBottomOf="@+id/screenText" app:layout_constraintBottom_toTopOf="@+id/button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" />And in MainActivity, I added a custom ArrayAdapter to the SearchableSpinner (I put "getApplicationContext()" instead of "this" because it's in another block):
SearchableSpinner searchableSpinner = findViewById(R.id.spinner_view); String[] dropDownArray = foodsMap.values().toArray(new String[0]); ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, dropDownArray) { @Override public Filter getFilter() { return new android.widget.Filter() { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); List<String> suggestions = new ArrayList<>(); if (constraint == null || constraint.length() == 0) { suggestions.addAll(items); } else { String[] queryWords = constraint.toString().toLowerCase().split(" "); for (String item : items) { String lowerItem = item.toLowerCase(); boolean allWordsMatch = true; for (String word : queryWords) { if (!lowerItem.contains(word)) { allWordsMatch = false; break; } } if (allWordsMatch) { suggestions.add(item); } } } results.values = suggestions; results.count = suggestions.size(); return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { clear(); if (results.count > 0) { addAll((List<String>) results.values); notifyDataSetChanged(); } } }; } }; adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); searchableSpinner.setAdapter(adapter);But it does not work. It completely empties when I enter "red" followed by a space.
Is there something else I need to override or add?
