我的可过滤适配器正在查找结果但未显示

时间:2018-03-31 21:16:14

标签: android recycler-adapter android-filterable

我已在Filterable中实施RecyclerView.Adapter<>,以按名称或地区搜索特定位置。我在performFiltering()中的Filter方法的每次迭代中记录结果,如此filteredLocationList是一个类变量:

private class LocationsAdapter extends RecyclerView.Adapter<LocationsAdapter.MyViewHolder> implements Filterable {
    private List<Location> locationList;
    private List<Location> filteredLocationList;

    LocationsAdapter(List<Location> locationList) {
        this.locationList = locationList;
        this.filteredLocationList = locationList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.view_location_row, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Location location = locationList.get(position);
        holder.name.setText(location.locName);
        holder.district.setText(location.locDistrict);

    }

    @Override
    public int getItemCount() {
        return filteredLocationList.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                String searchTerm = constraint.toString().toLowerCase();
                Log.w(TAG, "search " + searchTerm);
                if (searchTerm.isEmpty()) {
                    filteredLocationList = locationList;
                } else {
                    List<Location> filteredList = new ArrayList<>();
                    for (Location location : locationList) {
                        if (location.locName.toLowerCase().contains(searchTerm)) {
                            Log.i("location search", location.locName);
                            filteredList.add(location);
                        }
                    }
                    filteredLocationList = filteredList;
                }

                FilterResults searchResults = new FilterResults();
                searchResults.values = filteredLocationList;
                return searchResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                filteredLocationList = (ArrayList<Location>) results.values;
                notifyDataSetChanged();
            }
        };
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView name, district;

        MyViewHolder(View view) {
            super(view);
            name = view.findViewById(R.id.name);
            district = view.findViewById(R.id.district);
        }
    }
}

日志语句显示正在找到位置,但列表未相应更新。可能是造成这种情况的原因。

1 个答案:

答案 0 :(得分:-1)

在参考this教程后,我对getFilter()publishResults()方法进行了一些更改,这些方法似乎已经完成了。搜索后分配搜索结果的值和计数。在publishResults中,使用了适配器 getFilter()

searchResults.values = filteredLocationList;
searchResults.count = filteredLocationList.size();

publishResults()

adapter.filteredLocationList = (ArrayList<Location>) results.values;
adapter.notifyDataSetChanged();