当我尝试在回收站视图中过滤列表时,为什么filterResults为null?

时间:2019-09-08 00:07:07

标签: android recycler-adapter

我确实过滤了回收站视图,但是当涉及到“ publishResults”功能时,应用程序失败,因为“尝试调用接口方法'java.lang.Object [] java.util.Collection.toArray()”为null对象引用”

在执行以下几行之前,我发现它涉及到'publishResults':

 FilterResults results = new FilterResults();
        results.values = filterList;
        return results;

我不知道为什么

这些是适配器:

 List<Place> places;
 List<Place> allPlaces;

public PlaceAdapter(List<Place> places) {
    this.places = places;
    allPlaces = new ArrayList<>(places);
}

@NonNull
@Override
public PlaceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //this method is called for every
    // new line that is being created
    // - numbers of rows can be displayed in device+1
    View rowView = View.inflate(parent.getContext(),
            R.layout.place_row,
            null);

    return new PlaceViewHolder(rowView);
}

@Override
public void onBindViewHolder(@NonNull PlaceViewHolder holder, int position) {
    Place place = places.get(position);
    holder.txt_name_place.setText(place.getNamePlace());
    holder.txt_address.setText(place.getAddress());
    holder.txt_city.setText(place.getCity());

}

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

@Override
public Filter getFilter() {
    return filter;
}

private Filter filter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        List<Place> filterList = new ArrayList<>();
        if (charSequence == null || charSequence.length() == 0) {
            filterList.addAll(allPlaces);
        } else {
            String filterPattern = charSequence.toString().toLowerCase().trim();
            for (Place place : places) {
                if (place.getNamePlace().toLowerCase().contains(filterPattern)
                        || place.getNickname().toLowerCase().contains(filterPattern)
                        || place.getAddress().toLowerCase().contains(filterPattern)
                        || place.getCity().toLowerCase().contains(filterPattern)) {
                    filterList.add(place);
                }
            }
        }
        FilterResults results = new FilterResults();
        results.values = filterList;
        return results;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

            places.clear();
            places.addAll((List) filterResults.values);
            notifyDataSetChanged();


    }
};

public class PlaceViewHolder extends RecyclerView.ViewHolder {

    TextView txt_name_place;
    TextView txt_address;
    TextView txt_city;

    public PlaceViewHolder(@NonNull View itemView) {
        super(itemView);
        txt_name_place = itemView.findViewById(R.id.txt_name_place);
        txt_address = itemView.findViewById(R.id.txt_address);
        txt_city = itemView.findViewById(R.id.txt_city);

    }
}

这是主要活动:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();

    searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            //todo check
            adapter.getFilter().filter(s);
            return false;
        }
    });
    return true;
}

1 个答案:

答案 0 :(得分:0)

最后我找到了问题的答案,这行错过了

results.count=filterList.size();

因此,如果filterList中有对象,则result.count!= 0