Quick guide to quick autocomplete textview
This demo shows how to speed up the original autocomplete textview assuming that we can work with ordered data.
Let's prepare a simple test environment, which demostrate the difference between the two versions. Then let's generate a few thousand test data, and create two textviews from which we will speed up the second one.
- public class Main extends Activity {
- AutoCompleteTextView mAutoCompleteTextViewOriginal;
- AutoCompleteTextView mAutoCompleteTextViewQuick;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mAutoCompleteTextViewOriginal = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextViewOriginal);
- ArrayAdapter<String> originalAdapter = new ArrayAdapter<String>(this, R.layout.autocomplete_listitem, values);
- mAutoCompleteTextViewOriginal.
setAdapter(originalAdapter); - mAutoCompleteTextViewQuick = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextViewQuick);
- BinaryArrayAdapter<String> binaryAdapter = new BinaryArrayAdapter<String>(this, R.layout.autocomplete_listitem, values);
- mAutoCompleteTextViewQuick.setAdapter(binaryAdapter);
- }
- nf.setMinimumIntegerDigits(length);
- nf.setMaximumIntegerDigits(length);
- nf.setGroupingUsed(false);
- for (int i = 0; i < values.length; i++) {
- values[i] = nf.format(i);
- }
- return values;
- }
- }
The elements are selected by the ArrayAdapter, and within that the ArrayFilter, which is an inner class. We can exchange this, if we overwrite the getFilter () method of the ArrayAdapter.
Unfortunately, we cannot give back the received result to the parent class because of the private mObjects member. After a half-day trial, a less elegant, but in a few minutes feasible solution was: we copy the whole ArrayAdapter class, and we exchange few rows which are necessary to the binary search.
The used search class is the following:
- public class BinarySearch<T> {
- List<T> mList;
- Comparator<String> mComparator;
- /**
- * Create Search list of item and comparator
- *
- * @param list
- * @param comparator
- */
- public BinarySearch(List<T> list, Comparator<String> comparator) {
- mList = list;
- mComparator = comparator;
- }
- /**
- * Select sub list with given prefix
- *
- * @param prefixString
- * @return
- */
- int first = findFirstIndex(0, mList.size() - 1, prefixString);;
- if (-1 == first) {
- return new ArrayList<T>();
- }
- int last = findLastIndex(0, mList.size() - 1, prefixString);
- return mList.subList(first, last + 1);
- }
- int size = end - start;
- if (size < 0) {
- return -1;
- }
- if (size == 0) {
- if (mComparator.compare(mList.get(start).toString(), prefixString) == 0) {
- return start;
- } else {
- return -1;
- }
- } else {
- int half = (start + end) / 2;
- T halfItem = mList.get(half);
- int result = mComparator.compare(halfItemString, prefixString);
- if (result > 0) {
- // Search in first half
- return findFirstIndex(start, half - 1, prefixString);
- } else if (result < 0) {
- // Search in second half
- return findFirstIndex(half + 1, end, prefixString);
- } else /* if (result == 0) */{
- // Search in first half
- return findFirstIndex(start, half, prefixString);
- }
- }
- }
- /**
- * Find last index
- *
- * @param array
- * @param prefixString
- * @return
- */
- int size = end - start;
- if (size < 0) {
- return -1;
- }
- if (size == 0) {
- if (mComparator.compare(mList.get(start).toString(), prefixString) == 0) {
- return start;
- } else {
- return -1;
- }
- } else {
- int half = (start + end + 1) / 2;
- T halfItem = mList.get(half);
- int result = mComparator.compare(halfItemString, prefixString);
- if (result > 0) {
- // Search in first half
- return findLastIndex(start, half - 1, prefixString);
- } else if (mComparator.compare(halfItemString, prefixString) < 0) {
- // Search in second half
- return findLastIndex(half + 1, end, prefixString);
- } else /* if (mComparator.compare(halfItem,
prefixString) == 0) */{ - // Search in second half
- return findLastIndex(half, end, prefixString);
- }
- }
- }
- }
This class can be used instead of the original ArrayFilter algorithm:
- if (prefix == null || prefix.length() == 0) {
- synchronized (mLock) {
- ArrayList<T> list = new ArrayList<T>(mOriginalValues);
- results.values = list;
- results.count = list.size();
- }
- } else {
- 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()
; - // // First match against the whole, non-splitted value
- // if (valueText.startsWith(prefixSt
ring)) { - // newValues.add(value);
- // } else {
- // final String[] words = valueText.split(" ");
- // final int wordCount = words.length;
- //
- // for (int k = 0; k < wordCount; k++) {
- // if (words[k].startsWith(prefixStr
ing)) { - // newValues.add(value);
- // break;
- // }
- // }
- // }
- // }
- // CHANGED TO
- BinarySearch<T> logSearch = new BinarySearch<T>(values, new Comparator<String>() {
- @Override
- return temp.compareTo(object2);
- }
- });
- final List<T> newValues = logSearch.select(prefixString);
- Log.d("Search", "count: " + newValues.size());
- results.values = newValues;
- results.count = newValues.size();
- }
The autocomplete textview can be used with much data even on older apparatus.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 45 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 45 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 45 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 45 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 45 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN