我正在尝试根据项目的直径筛选项目列表。直径位于产品名称的字符串中,该字符串是自定义对象的一个元素。到目前为止,当我从微调器中选择一个直径时,它正在清除列表中的每一项。
微调器逻辑
//Spinner
diameterSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object diameterFilter = spinnerAdapter.getItem(position);
dAdapter.getFilter().filter(diameterFilter.toString());
dAdapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
适配器上的筛选器代码
//FILTER
@Override
public Filter getFilter() {
return displayFilter;
}
private Filter displayFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Inventory> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0){
filteredList.addAll(displayedListFull);
}else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (Inventory item : displayedListFull){
if (item.getProductName().toLowerCase().contains(filterPattern)){
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
displayedList.clear();
displayedList.addAll((List)results.values);
notifyDataSetChanged();
}
};
1条答案
按热度按时间5cnsuln71#
改变
getItem
进入getItemAtPosition
在微调器回调中