I have a listView
that has two text fields ( nome
and local
) that are taken from a Empresa
object.
The examples scattered on the internet only show search
in listViews
normal with only one string . How do I search
search in the nome
text field within itemView
?
Code to search for% of Strings :
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
if (TextUtils.isEmpty(newText))
{
list.clearTextFilter();
}
else
{
list.setFilterText(newText);
}
return true;
}
private class MyListAdapter extends ArrayAdapter<Empresa> {
public MyListAdapter() {
super(Main.this, R.layout.item_view, empresaList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null)
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
//acha empresa
Empresa currentEmpresa = empresaList.get(position);
//preenche lista
TextView makeTxtEmpresa = (TextView) itemView.findViewById(R.id.item_txtNome);
makeTxtEmpresa.setText(currentEmpresa.getNome());
TextView makeTxtLocal = (TextView) itemView.findViewById(R.id.item_txtLocal);
makeTxtLocal.setText(currentEmpresa.getLocal());
return itemView;
}
}