I have a list view with a list item, which has few text views and a checkbox. Using action bar search, I need to filter out the list by a text view value.
This is my list which I need to filter by "Priority".
This is the method I used to filter the list from the data adopter.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
dataAdapter.getFilter().filter(newText);
System.out.println("on text chnge text: "+newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
dataAdapter.getFilter().filter(query);
System.out.println("on query submit: "+query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
return super.onCreateOptionsMenu(menu);
}
I have set the dataAdopter to my listview which contains few elements, i.e 3 text views and a check box.
// create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.task_info, taskList);
listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
But when I set the adapter in the following way; using just a string array of values, the filtering happens without any issues
String[] dataArray = new String[] {"High","Medium", "Low", "Medium", "High", "Low","High"};
listView = (ListView) findViewById(R.id.listview);
myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataArray);
listView.setAdapter(myAdapter);
Is there any method that I can use to filter the list by the value of a text view inside a list item? Or is there any alternative method to implement the desired functionality.