我想在android中的列表视图中搜索。这里是我的代码在android中的过滤器搜索listView
@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”来自搜索查询,结果保持为空,这是预期的结果。 我该如何解决它?
发布整个适配器 – Blackbelt
没用,包含点击操作代码 –