While working on my first app I’d like to give the user the ability to search the entries in a ListView (it’s in the UI-Thread).
For that, I added the SearchView into the Action Bar.
listView1.setTextFilterEnabled(true);
In an AsyncTask I set the Adapter to the listView:
protected void onPostExecute(boolean result) {
ListAdapter listadapter= new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, nameList);
listview1.setAdapter(listadapter);
}
Now the SearchView:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setOnQueryTextListener(this);
return true;
[...]
}
Also I added these functions:
public boolean onQueryTextChange(String newText) {
ListView listview1= (ListView) findViewById(R.id.mylist);
listview1.setFilterText(newText);
//mFilter.filter(newText);
return false;
}
public boolean onQueryTextSubmit(String query) {
ListView listview1= (ListView) findViewById(R.id.mylist);
listview1.setFilterText(query);
}
Now the filtering is working fine, but this overlay with the searched char sequence is hiding the filtered data -> it doesn’t look well.
As I read everywhere, there’s no simple possibility to turn this overlay off.
So I changed my code a bit:
onPostExecute(blaaaaa) { [...]
//Filter mFilter= = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, nameList).getFilter();
and the two functions:
//mFilter.filter(the_entered_text);
((ArrayAdapter<String>)listAdapter).getFilter().filter(query);
((ArrayAdapter<String>)listAdapter).notifyDataSetChanged();
My question is now, how can I let the listView get updated?
That “listview1.notifyDataSetChanged()” is no option for me, because it isn’t available for my ListAdapter.
EDIT: cast the adapter, as in an answer postet, now it works, but it filters so: entered text= “ka” -> all entries, which start with “ka” are displayed, not the ones which contain “ka”.
listview1.getAdapter().notifyDataSetChanged()Simples