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();
}
}
}
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();
}
}
}
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With