Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No results with custom ArrayAdapter Filter

I'm using the ArrayAdapter on a AutoCompleteTextView. results.values has the expected value but I get no list on the UI.

public class CustomArrayAdapter extends ArrayAdapter<String> implements Filterable {
    private final Object mLock = new Object();
    private CustomFilter mFilter;
    public ArrayList<String> mItems;
    public ArrayList<String> mFiltered;

    public CustomArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        mItems = new ArrayList<String>();
        mFiltered = new ArrayList<String>();
    }

    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new CustomFilter();
        }
        return mFilter;
    }

    @Override
    public void add(String s) { 
        mItems.add(s);
    }

    private class CustomFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (mItems == null) {
                synchronized (mLock) {
                    mItems = new ArrayList<String>();
                }
            }

            if (constraint == null || constraint.length() == 0) {
                synchronized (mLock) {
                    results.values = mItems;
                    results.count = mItems.size();
                }
            } else {
                final ArrayList<String> newItems = new ArrayList<String>();

                for (int i = 0; i < mItems.size(); i++) {
                    final String item = mItems.get(i);
                    if(item.contains(constraint)) {
                        newItems.add(item);
                    }
                }
                results.values = newItems;
                results.count = newItems.size();
            }

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            mFiltered.clear();
            mFiltered.addAll((ArrayList<String>) results.values);
            notifyDataSetChanged();
        }

    }
}
like image 342
Pim Reijersen Avatar asked Dec 04 '22 19:12

Pim Reijersen


2 Answers

Looks like overriding a couple more functions did the job. Full source of working version:

public class CustomArrayAdapter extends ArrayAdapter<String> implements Filterable {
private List<String> mOrigionalValues;
private List<String> mObjects;
private Filter mFilter;

public CustomArrayAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    mOrigionalValues = new ArrayList<String>();
    mObjects = new ArrayList<String>();
}

public void add(String object) {
    mOrigionalValues.add(object);
    this.notifyDataSetChanged();
}

@Override
public int getCount() {
    return mObjects.size();
}

@Override
public String getItem(int position) {
    return mObjects.get(position);
}

public Filter getFilter() {
    if (mFilter == null) {
        mFilter = new CustomFilter();
    }
    return mFilter;
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

private class CustomFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();

        if(constraint == null || constraint.length() == 0) {
            ArrayList<String> list = new ArrayList<String>(mOrigionalValues);
            results.values = list;
            results.count = list.size();
        } else {
            ArrayList<String> newValues = new ArrayList<String>();
            for(int i = 0; i < mOrigionalValues.size(); i++) {
                String item = mOrigionalValues.get(i);
                if(item.contains(constraint)) {
                    newValues.add(item);
                }
            }
            results.values = newValues;
            results.count = newValues.size();
        }       

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        mObjects = (List<String>) results.values;
        Log.d("CustomArrayAdapter", String.valueOf(results.values));
        Log.d("CustomArrayAdapter", String.valueOf(results.count));
        notifyDataSetChanged();
    }

}

}
like image 98
Pim Reijersen Avatar answered Jan 03 '23 12:01

Pim Reijersen


You need overwrite getCount method in CustomArrayAdapter:

@Override
public int getCount() {
    return mFiltered.size();
}

And modify your publishResults method code:

protected void publishResults(CharSequence constraint,FilterResults results) {
            mFiltered = (List<String>) results.values;
            notifyDataSetChanged();
        }
like image 38
Ricardas Avatar answered Jan 03 '23 14:01

Ricardas