0

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 Answer 1

0

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

3
  • Can you expand on that a bit? I'm still a novice here. How would I handle that in publishResults and get the listview to update based on what I'm filtering it on? Jul 3, 2014 at 21:38
  • In the constructor you now assign results only to one list. Assign them to both lists. Let getView and the other functions retrieve from the filtered list. And finally in publishResults when results.count==0 assign the custom list to the filtered list before notifying that the data set has changed.
    – greenapps
    Jul 3, 2014 at 21:45
  • Thank you, your responses lead me to discover my issue. Jul 4, 2014 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.