自定义ArrayAdapter过滤器异常:publishResults方法故障求助
Hey there, let's tackle this ArrayAdapter Filter issue you're facing—it’s super frustrating when your filtering logic checks out during debugging but publishResults throws an unexpected error. Let’s break down the common culprits and fix this step by step.
Common Reasons for publishResults Exceptions
From your code snippet, I notice you’re assigning object = objects directly—this is a common source of issues because you’re referencing the original list instead of creating a separate copy. Here are the top issues to check:
- Direct reference to original dataset: If you modify the same list used by the adapter, you can trigger concurrent modification errors or unexpected data state changes.
- Null or invalid FilterResults: If
results.valuesis null, or the type doesn’t match yourKataloglist, you’ll get aClassCastExceptionorNullPointerException. - Missing dataset update: Forgetting to replace the adapter’s displayed dataset or call
notifyDataSetChanged()after filtering.
Fixed Adapter Implementation
Here’s a corrected version of your KatalogAdapter that addresses these issues. We’ll use two separate lists: one for the original unfiltered data, and one for the currently displayed filtered data.
public class KatalogAdapter extends ArrayAdapter<Katalog> implements Filterable { // Original unfiltered dataset (never modified after initialization) private List<Katalog> originalItems; // Currently displayed filtered dataset private List<Katalog> displayedItems; private KatalogFilter filterInstance; public KatalogAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<Katalog> objects) { super(context, resource, objects); // Create copies of the original list to avoid reference issues originalItems = new ArrayList<>(objects); displayedItems = new ArrayList<>(objects); } @Override public int getCount() { // Return size of the displayed filtered list return displayedItems.size(); } @Override public Katalog getItem(int position) { return displayedItems.get(position); } @Override public Filter getFilter() { if (filterInstance == null) { filterInstance = new KatalogFilter(); } return filterInstance; } private class KatalogFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); List<Katalog> tempFilteredList = new ArrayList<>(); String filterText = (constraint == null) ? "" : constraint.toString().toLowerCase().trim(); if (filterText.isEmpty()) { // No filter applied: use full original dataset tempFilteredList.addAll(originalItems); } else { // Your existing filtering logic (debugged to work) for (Katalog item : originalItems) { // Replace with your actual Katalog property check if (item.getYourProperty().toLowerCase().contains(filterText)) { tempFilteredList.add(item); } } } results.values = tempFilteredList; results.count = tempFilteredList.size(); return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { try { // Clear the current displayed list and update with filtered results displayedItems.clear(); if (results.values != null) { displayedItems.addAll((List<Katalog>) results.values); } // Notify the adapter to refresh the UI notifyDataSetChanged(); } catch (ClassCastException e) { // Catch type mismatch errors (helpful for debugging) Log.e("KatalogAdapter", "Failed to cast FilterResults values", e); } catch (NullPointerException e) { // Catch null reference errors Log.e("KatalogAdapter", "Null value in FilterResults", e); } } } }
Debugging Tips to Pinpoint the Issue
If you still hit exceptions after using this code, try these steps:
- Add breakpoints in publishResults: Check if
results.valuesis null, or if it’s a different type thanList<Katalog>. - Log FilterResults details: Print
results.countandresults.valuesto verify the filtered data is valid before updating the adapter. - Check thread safety: Ensure no other threads are modifying
originalItemswhile filtering is in progress. - Verify Katalog class: Make sure your
Katalogclass doesn’t have unexpected null values in the properties you’re filtering on.
内容的提问来源于stack exchange,提问作者Мирослав Секулић

