Hello I need filter an ArrayList
I have a ArrayList with following items:
"Hello world"
"Hello man"
"Hello woman"
"Goodbye world"
"Goodbye man"
"Goodbye woman"
then on TextBox I added a textchanged listener (Android).What i want is if I write "he wo" separated by space, it displays "Hello world" and "Hello woman".
I've been fighting with this about an hour and only I got is that if I write "he wo" I get the following items: "Hello world", "Hello man", "Hello woman", "Goodbye world".
Here is the code
et.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
//listabusClientes = new ArrayList();
paraulesBuscar = s.toString().split(" "); //Array of string
sortarray = new ArrayList(); //ArrayList of class busCliente
sortarray.clear();
for(int x = 0; x < paraulesBuscar.length; x++){
sortarray.clear();
for (int i = 0; i < listabusClientes.size(); i++)
{
if ((listabusClientes.get(i).getNom().toUpperCase().indexOf(paraulesBuscar[x].toUpperCase()) != -1) || (listabusClientes.get(i).getCodiClient().toUpperCase().indexOf(paraulesBuscar[x].toUpperCase()) != -1))
{
sortarray.add(listabusClientes.get(i));
}
}
}
lv.setAdapter(new ListViewAdapter(buscar_client.this,
R.layout.simple_list_item_1, sortarray));
}
});
Edited algorithm
paraulesBuscar = s.toString().split(" ");
sortarray = new ArrayList();
sortarray.clear();
for(int x = 0; x < paraulesBuscar.length; x++){
sortarray.clear();
for (int i = 0; i < listabusClientes.size(); i++)
{
String[] clientSplit = listabusClientes.get(i).getNom().split(" ");
for(int cs = 0; cs < clientSplit.length; cs++){
System.out.println(clientSplit[cs]);
if (clientSplit[cs].toUpperCase().contains(paraulesBuscar[x].toUpperCase()))
{
sortarray.add(listabusClientes.get(i));
}
}
}
}