Filtering a ListView with SearchView and custom adapter

青春壹個敷衍的年華 提交于 2019-12-11 21:08:12

问题


I've been unable to get my ListView to sort. When I type in the SearchView, nothing happens. My ListView has no changes. I've been comparing examples found here and elsewhere with no luck. I've confirmed through debugging that the filter actually does work, I can see the filteredArray being populated with what filtered data, but again, no ListView change.

Here is what I have so far. In MainActivity.class..

public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

        searchView.setSubmitButtonEnabled(false);
        searchView.setQueryHint("Enter #..");
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

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

            @Override
            public boolean onQueryTextChange(String s) {
                ListView listView = (ListView) findViewById(R.id.main_listView);
                listView.setTextFilterEnabled(true);

                if (s.isEmpty()) {
                    listView.clearTextFilter();
                } else {
                    customAdapter.getFilter().filter(s);
                }

                return true;
            }
        };

        searchView.setOnQueryTextListener(queryTextListener);

        return super.onCreateOptionsMenu(menu);
    }

I create the customAdapter object like this and listCustom contains all the CustomResults classes.

customAdapter = new CustomAdapter(MainActivity.this, listCustom);

CustomAdapter:

public class CustomAdapter extends BaseAdapter implements Filterable {
    private static ArrayList<CustomResults> customArrayList;
    private static ArrayList<CustomResults> filteredArrayList;

    private LayoutInflater mInflater;

    public CustomAdapter(Context context, ArrayList<CustomResults> results) {
        customArrayList = results;
        mInflater = LayoutInflater.from(context);
    }

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

    public Object getItem(int position) {
        return customArrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_row, null);

            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.num = (TextView) convertView.findViewById(R.id.number);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(customArrayList.get(position).getName());
        holder.num.setText(customArrayList.get(position).getNum());

        return convertView;
    }

    static class ViewHolder {
        TextView name;
        TextView num;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                if (results.count == 0) {
                    notifyDataSetInvalidated();
                } else {
                    filteredArrayList = (ArrayList<CustomResults>) results.values;
                    notifyDataSetChanged();
                }

            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                String filterString = constraint.toString().toLowerCase();
                FilterResults results = new FilterResults();

                int count = customArrayList.size();

                ArrayList<CustomResults> filteredList = new ArrayList<CustomResults>();

                for (CustomResults custom : customArrayList) {
                    filteredList.add(custom);
                }

                String filterableString ;

                for (int i = 0; i < count; i++) {
                    filterableString = customArrayList.get(i).getNum();
                    if (filterableString.toLowerCase().contains(filterString)) {
                        filteredList.add(customArrayList.get(i));
                    } else if (customArrayList.get(i).getName().toLowerCase().contains(filterString)) {
                        filteredList.add(customArrayList.get(i));
                    }
                }

                results.values = filteredList;
                results.count = filteredList.size();

                return results;
            }
        };

        return filter;

    }

回答1:


getView() and other functions retrieve always from customArrayList and never from filteredArrayList.



来源:https://stackoverflow.com/questions/24562269/filtering-a-listview-with-searchview-and-custom-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!