How to hide filter popup in ListView in Android?
I found an easier solution that works for me on this blog:
http://blog.jamesbaca.net/?p=128
When you create ArrayAdapter
to fill ListView
, just keep a link to your Filter and then you can just change Filter on ArrayAdapter
instead ListView
.
* In Xamarin, you can simply use arrayAdapter.InvokeFilter("my text");
source to share
Create a filter (you can use a different adapter)
ContentAdapter adapter = new ContentAdapter(this, android.R.layout.simple_list_item_1,
s);
list1.setAdapter(adapter);
Filter f = adapter.getFilter();
Then use filter inside 'onQueryTextChange' instead of commented code
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
//list1.clearTextFilter();
f.filter(null);
} else {
//list1.setFilterText(newText.toString());
f.filter(newText);
}
return true;
}
source to share
Hi like Vatsal said you can create a filter from an adapter. So I figured I created the ArrayAdapter as a class variable.
// ...
private void setListAdapter() {
List<Word> words = Dictionary.getInstance(this).getWords();
arrayAdapter = new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
words);
// Sets the List Adapter that we are using
this.setListAdapter(arrayAdapter);
}
// ...
then from this class variable in onQueryTextChange or onQueryTextSubmit you can get the filter this way
//...
public boolean onQueryTextChange(String query) {
// By doing this it removes the popup completely which was arising from using the this.getListView().setFilterText(query) method
arrayAdapter.getFilter().filter(query);
return true;
}
//...
source to share