Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

android - 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();
        }

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
    }

}

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...