我正在尝试在 android 的列表视图中搜索。这是我的代码
@Override
public Filter getFilter(){
return new Filter(){
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults result = new FilterResults();
if (constraint != null && constraint.toString().length() > 0) {
constraint = constraint.toString();
List<FoodCell> founded = new ArrayList<FoodCell>();
for(FoodCell item: allItemFood){
if(item.cellText.toString().contains(constraint)){
founded.add(item);
}
}
result.values = founded;
result.count = founded.size();
}else
result.count = 0;
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
if(results.count != 0){
for (FoodCell item : (List<FoodCell>) results.values) {
add(item);
}
}
notifyDataSetChanged();
}
};
}
@Override
public boolean onQueryTextChange(String newText) {
//check if user deleted a character
if(length > newText.length()){
allItemFood = allItemFoodTmp;
}
if (TextUtils.isEmpty(newText)) {
adapter = new CustomListViewAdapter(this, R.layout.listfood,allItemFood);
getListView().setAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
} else {
adapter.getFilter().filter(newText);
}
length = newText.length();
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
假设我们在列表中有以下项目,aa,ab,ac 当我输入文本“aaz”作为搜索查询时,它显示空列表,这是真的,但是当我从搜索查询中删除字符“z”时,结果仍然是空的结果是假的。我该如何解决?