You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义ArrayAdapter过滤器异常:publishResults方法故障求助

Troubleshooting Your ArrayAdapter Filter's publishResults Exception

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.values is null, or the type doesn’t match your Katalog list, you’ll get a ClassCastException or NullPointerException.
  • 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:

  1. Add breakpoints in publishResults: Check if results.values is null, or if it’s a different type than List<Katalog>.
  2. Log FilterResults details: Print results.count and results.values to verify the filtered data is valid before updating the adapter.
  3. Check thread safety: Ensure no other threads are modifying originalItems while filtering is in progress.
  4. Verify Katalog class: Make sure your Katalog class doesn’t have unexpected null values in the properties you’re filtering on.

内容的提问来源于stack exchange,提问作者Мирослав Секулић

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:15:02