Troutmonkey
Troutmonkey

Reputation: 21

AutoCompleteTextView not showing all relevant suggestions

I am making an Deck Building app using an AutoCompleteTextView and everything appeared to be fine until one of my users reported it was not working correctly.

The view should auto-complete based on a list of card names provided, but doesn't seem to show all of the relevant options.

For example the following names are in the view

"Flooding at Sweet Apple Acres" "Save Sweet Apple Acres" "Sweet Apple Acres" "Sweet and Kind"

Typing "Sweet" into the view will show all of these values, but typing "Sweet " (with a trailing space) or "Sweet A" will only return the results "Sweet Apple Acres" and "Sweet And Kind" even though all of the other options are valid.

If there is a space in the text field it seems to ignore any auto-complete options that don't start with the given text. Is there any way to change this? I'm using the basic AutoCompleteTextView using default settings.

AutoCompleteTextView autoText = (AutoCompleteTextView) view.findViewById(R.id.card_name);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_dropdown_item_1line, CardDatabase.CARD_NAMES);
        autoText.setAdapter(adapter);

Upvotes: 0

Views: 1148

Answers (2)

Troutmonkey
Troutmonkey

Reputation: 21

Immediately after posting I had a revelation and reworded my question to google and came across this AutoCompleteTextView doesn't show dropdown when I press space after typing a complete word

which linked to this https://github.com/luksprog/DroidPlayground/blob/95d127a0fdd1c7c2520650081fadedb4d54ffe32/src/com/luksprog/dp/adapter/FilterWithSpaceAdapter.java

and solved my issue.

Upvotes: -1

Sandeep Singh
Sandeep Singh

Reputation: 1127

You can use Custom adapter for this

public class CustomAdapter extends BaseAdapter implements Filterable {


private Context context;


private List<String> originalData;
private List<String> filteredData;

private ItemFilter mFilter = new ItemFilter();

public CustomAdapter(List<String> itemList, Context ctx) {

    originalData = new ArrayList<String>();
    filteredData = new ArrayList<String>();

    filteredData = itemList;
    originalData = itemList;

    context = ctx;
}

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

@Override
public Object getItem(int position) {
    return filteredData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(android.R.layout.simple_dropdown_item_1line, null);
    }

    final TextView text = (TextView) convertView.findViewById(android.R.id.textView);

    text.setText(filteredData.get(position));

    return convertView;
}

public Filter getFilter() {
    return mFilter;
}

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();

        Filter.FilterResults results = new FilterResults();

        final List<String> list = originalData;

        int count = list.size();
        final ArrayList<String> nlist = new ArrayList<String>(count);

        String filterableString;

        for (int i = 0; i < count; i++) {

            filterableString = list.get(i);

            if (filterableString.toLowerCase().startsWith(filterString)) {
                nlist.add(filterableString);
            }
        }

        results.values = nlist;
        results.count = nlist.size();

        return results;
    }

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

}

}

Upvotes: 2

Related Questions