你好我需要过滤一个 ArrayList
我有一个包含以下项目的ArrayList :
“你好世界” “你好男人” “你好女人” “再见世界” “再见男人” “再见女人”
然后在TextBox上我添加了一个textchanged监听器(Android)。我想要的是如果我写“he wo”用空格分隔,它会显示“Hello world”和“Hello woman”。
我已经为此奋斗了大约一个小时,只有我得到的是,如果我写“he wo”,我会得到以下项目:“Hello world”,“Hello man”,“Hello woman”,“Goodbye world”。这是代码
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<busCliente>();
paraulesBuscar = s.toString().split(" "); //Array of string
sortarray = new ArrayList<busCliente>(); //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));
}
});
编辑算法
paraulesBuscar = s.toString().split(" ");
sortarray = new ArrayList<busCliente>();
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));
}
}
}
}