尝试使用edittext过滤我的listview,每当我输入一些文本时listview变为空白,任何人都可以告诉我我做错了什么以及我如何解决这个问题
search.addTextChangedListener(new TextWatcher (){
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
String searchString = cs.toString();//wsSearch.getText().toString();
/* if(searchString.length() != 2) {
adapter2 = new Adapter(MainActivity.this, array);
listView.setAdapter(adapter2);
return;
}*/
for (int i = 0; i < array.size(); i++)
{
String currentString = array.get(i).getTitulo();
if (searchString.equalsIgnoreCase(currentString))
{
mTemp.add(array.get(i));
}
}
adapter2 = new Adapter(MainActivity.this, mTemp);
listView.setAdapter(adapter2);
}
});
}
答案 0 :(得分:1)
试试这个:
search.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String searchString = arg0.toString();//wsSearch.getText().toString();
/* if(searchString.length() != 2) {
adapter2 = new Adapter(MainActivity.this, array);
listView.setAdapter(adapter2);
return;
}*/
mTemp.clear()
for (int i = 0; i < array.size(); i++)
{
String currentString = array.get(i).getTitulo();
if (currentString.contains(searchString))
{
mTemp.add(array.get(i));
}
}
adapter2 = new Adapter(MainActivity.this, mTemp);
listView.setAdapter(adapter2);
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
//BAPTISMAL_SONG.this.adapter2.getFilter().filter(cs);
}
});
希望这有帮助。