AutoCompleteTextView force to show all items
Basically, after 5-6 hours of experimentation to understand how the damn filter works, I wrote my own adapter which does exactly what I want:
public class burtuAdapteris extends ArrayAdapter<String> implements Filterable {
ArrayList<String> _items = new ArrayList<String>();
ArrayList<String> orig = new ArrayList<String>();
public burtuAdapteris(Context context, int resource, ArrayList<String> items) {
super(context, resource, items);
for (int i = 0; i < items.size(); i++) {
orig.add(items.get(i));
}
}
@Override
public int getCount() {
if (_items != null)
return _items.size();
else
return 0;
}
@Override
public String getItem(int arg0) {
return _items.get(arg0);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if(constraint != null)
Log.d("Constraints", constraint.toString());
FilterResults oReturn = new FilterResults();
/* if (orig == null){
for (int i = 0; i < items.size(); i++) {
orig.add(items.get(i));
}
}*/
String temp;
int counters = 0;
if (constraint != null){
_items.clear();
if (orig != null && orig.size() > 0) {
for(int i=0; i<orig.size(); i++)
{
temp = orig.get(i).toUpperCase();
if(temp.startsWith(constraint.toString().toUpperCase()))
{
_items.add(orig.get(i));
counters++;
}
}
}
Log.d("REsult size:" , String.valueOf(_items.size()));
if(!counters)
{
_items.clear();
_items = orig;
}
oReturn.values = _items;
oReturn.count = _items.size();
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
And it's simple to use, just replace original adapter with this:
final burtuAdapteris fAdapter = new burtuAdapteris(this, android.R.layout.simple_dropdown_item_1line, liste);
In my case liste is: ArrayList<String> liste = new ArrayList<String>();
To stop adapter from filtering you can set high enough treshold on your textview:
autoCompleteTextView.setThreshold(100);
Hope it helps.
You don't define the "moment" when you want to display all the results, so I hope this fits. But try something like this:
AutoCompleteTextView autoComplete;
String savedText;
public void showAll() {
savedText = autoComplete.getText().toString();
autoComplete.setText("");
autoComplete.showDropDown();
}
public void restore() {
autoComplete.setText(savedText);
}
This works for me perfectly, this is an easy way to resolve the problem:
final ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, usernameLists);
etUsername.setThreshold(1);
etUsername.setAdapter(adapter);
etUsername.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
if (usernameLists.size() > 0) {
// show all suggestions
if (!etUsername.getText().toString().equals(""))
adapter.getFilter().filter(null);
etUsername.showDropDown();
}
return false;
}
});