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

RecyclerView筛选功能失效:输入搜索内容无响应且无报错

Fixing RecyclerView Filtering Not Working with SearchView

Hey, let's fix this RecyclerView filtering issue step by step. I've gone through your code and spotted a few key problems that are causing the list to not update when you use the SearchView.


1. Incomplete SearchView Listener Implementation

Your SearchView.OnQueryTextListener only has an empty onQueryTextSubmit method, but real-time filtering relies on the onQueryTextChange method. Plus, you never actually trigger the adapter's filter logic.

Update your searchView.setOnQueryTextListener in UniversityPick activity like this:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        // Trigger filter when user submits the query
        adapter.getFilter().filter(query);
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        // Trigger filter as the user types
        adapter.getFilter().filter(newText);
        return true;
    }
});

2. RecyclerView Never Gets Attached to the Adapter

You create an instance of UniversitiesAdapter, but you never set it to your recyclerView. Without this binding, the adapter can't update the RecyclerView's display.

Add this line after initializing the adapter (make sure it's after your loadUniversities method finishes loading data):

recyclerView.setAdapter(adapter);

If loadUniversities loads data asynchronously (like a network call), make sure to initialize and set the adapter after the data is loaded. For example:

// Inside your data loading callback (e.g., after parsing the JSON response)
universitiesList.addAll(parsedUniversities);
adapter = new UniversitiesAdapter(UniversityPick.this, universitiesList);
recyclerView.setAdapter(adapter);

3. List Reference Issue in the Adapter

In your adapter's constructor, you assign universityFilter = universitiesList directly—this means both variables point to the same list in memory. When you filter, you're modifying the original data set, which breaks future filtering attempts.

Fix the adapter constructor to create copies of the list:

public UniversitiesAdapter(Context mCtx, ArrayList<Universities> universitiesList){
    this.mCtx = mCtx;
    // Save a copy of the original data to avoid modifying it during filtering
    this.universitiesList = new ArrayList<>(universitiesList);
    // Initialize filtered list with a copy of the original data
    this.universityFilter = new ArrayList<>(universitiesList);
}

Also, update the performFiltering method to reset to a copy of the original data when the search query is empty:

@Override
protected FilterResults performFiltering(CharSequence charSequence) {
    String charString = charSequence.toString();
    if (charString.isEmpty()) {
        // Reset to a copy of the original data, not a direct reference
        universityFilter = new ArrayList<>(universitiesList);
    } else {
        ArrayList<Universities> filteredList = new ArrayList<>();
        for (Universities universities : universitiesList) {
            if (universities.getUniversityName().toLowerCase().contains(charString.toLowerCase())) {
                filteredList.add(universities);
            }
        }
        universityFilter = filteredList;
    }
    FilterResults filterResults = new FilterResults();
    filterResults.values = universityFilter;
    return filterResults;
}

4. Verify Data Loading Logic

Double-check your loadUniversities method to ensure it's correctly populating the universitiesList with data. If it's a network request, confirm that the data is being parsed and added to the list before initializing the adapter.


After applying these fixes, your RecyclerView should update in real-time as you type in the SearchView, showing only the universities that match your query.

内容的提问来源于stack exchange,提问作者Mathiew Abbas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 03:28:32