I am attempting to filter a list of items base on their diameters. The diameter is found within the string of the product name which is an element of the custom object. So far when I select a diameter from the spinner it is clearing every item from the list.
Spinner Logic
//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 code on Adapter
//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();
}
};
Change getItem
into getItemAtPosition
in the spinner callback
diameterSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object diameterFilter = spinnerAdapter.getItemAtPosition(position); // here is the change
dAdapter.getFilter().filter(diameterFilter.toString());
dAdapter.notifyDataSetChanged();
}
// rest of your code