在内部输入内容时,我的searchview / filter没有任何作用

时间:2018-11-21 00:17:42

标签: java android listview android-arrayadapter searchview

我已经为一个应用程序编写了代码,该应用程序旨在读取XML并使用读取的数据填充列表视图。数据存储在一个包含类中。我创建了一个适配器类以及一个自定义过滤器。由于某种原因,当我在searchView中键入内容时,什么也没有出现!我在youtube / google上浏览了几天,找不到任何东西!请帮忙!

private class ArticleAdapter extends ArrayAdapter<Article> implements Filterable {
        private List<Article> completeList = new ArrayList<>(articles);

        public ArticleAdapter(Context ctx) {

            super(ctx, 0);

        }

        @NonNull
        @Override
        public Filter getFilter() {
            return titleFilter;
        }


        public int getCount() {
            return articles.size();
        }

        public Article getItem(int position) {
            return articles.get(position);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.title_layout, parent, false);

            }


            TextView articleTitle = convertView.findViewById(R.id.titleId);

            Article article = getItem(position);
            if (article != null) {
                articleTitle.setText(getItem(position).title);
            }


            return convertView;


        }

        Filter titleFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                List<Article> suggestions = new ArrayList<>();
                if (constraint == null || constraint.length() == 0) {
                    suggestions.addAll(completeList);

                } else {
                    String filterPattern = constraint.toString().toLowerCase().trim();
                    for (Article title : completeList) {
                        if (title.getTitle().toLowerCase().contains(filterPattern)) {
                            suggestions.add(title);
                        }
                    }
                }
                results.values = suggestions;
                results.count = suggestions.size();

                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                clear();
                addAll((List) results.values);
                notifyDataSetChanged();

            }

            @Override
            public CharSequence convertResultToString(Object resultValue) {

                return ((Article) resultValue).getTitle();
            }
        };

       /* public long getItemId(int position) {
            return getItem(position).id;
        }
*/

    }


}

这是我的ArticleList类(主类)中的onCreateOptionsMenu方法

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_search, menu);
        item = menu.findItem(R.id.menuSearch);
        searchView = (SearchView) item.getActionView();


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

            @Override
            public boolean onQueryTextChange(String newText) {
                if (articles.isEmpty()) {

                }
                adapter.getFilter().filter(newText);

                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

1 个答案:

答案 0 :(得分:0)

我有一个客户列表,我必须按他们的名字搜索客户。所以这是代码。希望对您有所帮助。 :)

///全局定义了这两个列表

私有列表customerList =新的ArrayList <>();

私有列表searchCustomerList = new ArrayList <>();

在onCreate()中,我将此方法称为:addTextListener();

private void addTextListener()  {

    try {

        searchBar.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence query, int start, int before, int count) {

                query = query.toString().toLowerCase();
                searchCustomerList.clear();
                for (int i = 0; i < customerList.size(); i++) {
                    if (customerList.get(i) != null) {
                        String text = customerList.get(i).getCustomerName().toLowerCase();
                        if (text.toString().contains(query)) {
                            searchCustomerList.add(customerList.get(i));
                        }
                    }
                }
                Log.d("filterflower", "size: " + searchCustomerList.size());
                customerListAdapter.updateList(searchCustomerList);  // data set changed
            }
        });

    }catch (Exception e){
        e.printStackTrace();
    }
}

//这是我的回收站适配器中的updateList方法

 public void updateList(List<CustomerListDataModel> searchCustomerList) {
      customerList = searchCustomerList;
      notifyDataSetChanged();
    }