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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…