Android AutoCompleteTextView offers an alternative

Is it possible to have an AutoCompleteTextView that searches for a string but offers an alternative string?

For example, if the user enters "Yau" or "yauh", he suggests "Yáuh", so the user does not need to enter special characters.

+4
source share
3 answers

I had a desire to extend the ArrayAdapter, but it does not seem to be overwritten:

import java.util.ArrayList; import java.util.List; import android.content.Context; import android.widget.ArrayAdapter; import android.widget.Filter; import android.widget.Filterable; public class foreignAdapter<T> extends ArrayAdapter implements Filterable { String temp; String word; private List<T> mObjects; private final Object mLock = new Object(); private ArrayList<T> mOriginalValues; public foreignAdapter(Context context, int resource, T[] object) { super(context, resource, object); } private class ArrayFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence prefix) { FilterResults results = new FilterResults(); if (mOriginalValues == null) { synchronized (mLock) { mOriginalValues = new ArrayList<T>(mObjects); } } if (prefix == null || prefix.length() == 0) { synchronized (mLock) { ArrayList<T> list = new ArrayList<T>(mOriginalValues); results.values = list; results.count = list.size(); } } else { String prefixString = prefix.toString().toLowerCase(); final ArrayList<T> values = mOriginalValues; final int count = values.size(); final ArrayList<T> newValues = new ArrayList<T>(count); for (int i = 0; i < count; i++) { final T value = values.get(i); final String valueText = value.toString().toLowerCase(); /* Run for each character in the string */ for(int j=0;j<valueText.length();j++) { switch(valueText.charAt(j)) { case 'á': case 'ā': case 'à': temp+='a'; break; case 'í': case 'ī': case 'ì': temp+='i'; break; case 'è': case 'ē': case 'é': temp+='e'; break; case 'ó': case 'ō': case 'ò': temp+='o'; break; case 'ú': case 'ū': case 'ù': temp+='u'; break; case 'ģ': temp+='g'; break; default: temp+=word.charAt(j); break; } } // First match against the whole, non-splitted value if (temp.startsWith(prefixString)) { newValues.add(value); } else { final String[] words = temp.split(" "); final int wordCount = words.length; for (int k = 0; k < wordCount; k++) { if (words[k].startsWith(prefixString)) { newValues.add(value); break; } } } } results.values = newValues; results.count = newValues.size(); } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { //noinspection unchecked mObjects = (List<T>) results.values; if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } } } 
+1
source

I have not tried, but it will definitely be ...

You did not indicate whether your data is in an array or DB, so I'm going to accept an array (or list).

Cancel the getFilter() your adapter. (Check this question to see how)

Now, in this example (in the link), the author uses startsWith to filter the results. Instead, you will have to use your own comparison method, which will also consider these special characters. (as in, we equate á with a )

0
source

There is an AutoCompleteTextView here, so I assume you need:

 <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="MyAutoCompleteTextView" > </AutoCompleteTextView> 

Here's the official info: AutoCompleteTextView

I just saw a tutorial there, AutoCompleteTextView Tutorial

-1
source

Source: https://habr.com/ru/post/1381458/


All Articles