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
-
@lorealmichelle (Loreal Witherspoon ♔)I've just received an achievement: Tax Collector http://t.co/vektXV7M #Android #Androidgames #Gameinsight
9 min 15 sec ago -
@mawangbird (정진현)Start playing Paradise Island on Android http://t.co/QhMy4qEk #Android #Androidgames #Gameinsight http://t.co/FxWuH7D6
9 min 17 sec ago -
@babybluesnshn11 (Dawn wolf)I've just received an achievement: Parks Developer http://t.co/poJCRd2M #Android #Androidgames
9 min 25 sec ago -
@lyazi256 (lyazi)
The "Farm" collection in Big Business has been completed! 71 http://t.co/qPVxRX47 #android #gameinsight #androidgames
9 min 28 sec ago -
@adrianthompkins (Adrian Thompkins)#Android Stuff: S-Voice gets gagged: Samsung blocks leaked APKs http://t.co/TiWjmmux
9 min 28 sec ago
Poll
Useful resources
Android Development Projects
- Private project for Petterpp1 [WRD] by geomaster909
- Modifications on an Android Game by tymex
- app dev for techcity by dany2g
- Android App by dvlinh
- Iphone app by nabzyd
- Betfair Mobile App by Daaniel
- Need App Developer/Designer for iPhone, iPad by lplco
- Android Maps - Saving GeoLocations to MySql database. by adelshehri
- Prototype Android Apps display Information from database by getitonline
- Metal Weigh Calculator by mpmakwana



Comments
Great ArticlE >> ThX AloT
Great ArticlE >> ThX AloT ..
I ReallY EnjoyeD ReadinG it
It is very useful .. and u have great style
I hope u can visit my site below
And .. I,m Waiting for more
شات مصرى ,
شات مصرى ,
شات مصرى ,
شات مصرية ,
شات مصريه ,
دردشة مصرية ,
دردشة بنت مصرية ,
شات بنت مصرية ,
شات بنت مصر ,
دردشة بنت مصر ,
شات ,
دردشه مصريه ,
شات بنات مصر ,
دردشة بنات مصر ,
شات مصرى بنات ,
شات مصر ,
دردشة ,
شات اسكندرية ,
شات القاهرة ,
شات المنصورة ,
شات طنطا ,
شات السويس ,
شات الشرقية ,
شات المنوفية ,
شات الفيوم ,
شات بنى سويف ,
شات سوهاج ,
شات الصعيد ,
شات الزقازيق ,
شات جامعة القاهرة ,
شات جامعة اسكندرية ,
شات بنت السعودية ,
شات بنت البحرين ,
شات بنت الكويت ,
شات بنت فلسطين ,
شات بنت المغرب ,
شات بنت الامارات ,
شات بنت اليمن ,
شات بنت العراق ,
شات مصراوى ,
شات بنات ,
روتانا سينما ,
روتانا سينما مشاهدة مباشرة ,
روتانا سينما بث مباشر ,
روتانا سينما اونلاين ,
I hope u can find something useful About it .. Enjoy
Thanks .. lol i had to read it twice
شات صوتي | دردشة صوتية | كلام
شات صوتي
| دردشة صوتية
|
كلام
| شات كلام
|
دردشة كلام
| دردشة صوتية
|
شات صوتي
| شات
|
Chat Voice
| ahj w,jd
شات صوتي
| دردشة صوتية
|
شات صوتي
| دردشة صوتية
|
دردشه
| دردشة
|
صوتي
| صوتية
|
شات صوتي
| دردشة صوتية
|
شات صوتي
| دردشة صوتية
|
الكلام
| دردشه صوتيه
|
]v]am w,jdm
| ]v]ai w,jdi
شات صوتي
شات صوتي
شات صوتي