SearchView无法在Android中使用。为什么这样?

时间:2019-06-23 08:45:42

标签: android listview android-recyclerview adapter searchview

我在我的应用程序中使用带有标题的Recycleview,并且试图在我的应用程序中实现searchview,但是它不起作用。当我单击搜索视图并键入内容时,结果不会被过滤,这会在日志猫中得到。

2019-06-23 14:00:48.471 27193-27508/com.example.testapp W/Filter: An exception occured during performFiltering()!
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference
        at com.example.testapp.TitleAdapter$1.performFiltering(TitleAdapter.java:92)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:171)
        at android.os.HandlerThread.run(HandlerThread.java:65)

我已经呆了几个小时了,无法弄清楚我在做什么错。这是我的适配器。

public class TitleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable {

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    private List <ItemObject> list;
    private List <ItemObject> listFiltered;
    private Context context;

    public TitleAdapter(Context context, List <ItemObject> list) {
        this.context = context;
        this.list = list;
        this.listFiltered = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout, parent, false);
            return new HeaderViewHolder(layoutView);
        } else if (viewType == TYPE_ITEM) {
            View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
            return new ItemViewHolder(layoutView);
        }
        throw new RuntimeException("No match for " + viewType + ".");
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        ItemObject list = this.list.get(position);
        if (holder instanceof HeaderViewHolder) {
            ((HeaderViewHolder) holder).headerTitle.setText(list.getHeaderTitle());
        } else if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).titleText.setText(list.getPageName());
        }
    }

    public ItemObject getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position))
            return TYPE_HEADER;
        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
        ItemObject mObject = list.get(position);
        return mObject.isHeader();
    }

    @Override
    public Filter getFilter() {

        return new Filter() {
            protected FilterResults performFiltering(CharSequence constraint) {

                if (constraint == null || constraint.length() == 0){
                    listFiltered = list;
                } else {
                    String filterPattern = constraint.toString().toLowerCase().trim();
                    List<ItemObject> lstFilter = new ArrayList<>();
                    for (ItemObject row : list) {

                        // name match condition. this might differ depending on your requirement
                        // here we are looking for name or phone number match
                        if (row.getPageName().toLowerCase().contains(filterPattern)) {
                            lstFilter.add(row);
                        }
                    }

                    listFiltered = lstFilter;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = listFiltered;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults results) {
                listFiltered = (List <ItemObject>) results.values;
                // refresh the list with filtered data
                notifyDataSetChanged();
            }
        };

    }

    public class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView headerTitle;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            headerTitle =  itemView.findViewById(R.id.headerText);

        }
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder{

        TextView titleText;

        public ItemViewHolder(View itemView) {
            super(itemView);
            titleText = itemView.findViewById(R.id.titleText);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int position = getAdapterPosition();

                    Intent intent = new Intent(context, ViewerActivity.class);
                    intent.putExtra("PageName",list.get(position).getPageName());
                    intent.putExtra("PageUrl",list.get(position).getPageUrl());
                    context.startActivity(intent);
                }
            });
        }
    }
}

顺便说一句,如果您想知道这是第92行

 if (row.getPageName().toLowerCase().contains(filterPattern)) {
                            lstFilter.add(row);
                        }

1 个答案:

答案 0 :(得分:0)

如您所知,null上有getPageName()个异常,因此只需对其进行检查

if(row.getPageName()!=null && !row.getPageName().isEmpty())
 if (row.getPageName().toLowerCase().contains(filterPattern)) {
                            lstFilter.add(row);
                        }

,我看到您在list中有getItemCount()个对象,因此我假设主要的“列表”为list,因此您必须备份该对象,并在此行执行按照:

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults results) {
                //listFiltered = (List <ItemObject>) results.values;
                //back up `list` some where else for reloading the list without filter
                list.clear();
                list.addAll((List <ItemObject>) results.values);

                // refresh the list with filtered data
                notifyDataSetChanged();
            }

备份主数据并将过滤器放入