Wednesday, 18 July 2018

AutoCompleteTextView's performFiltering called all the time

I have the following adapter for my AutoCompleteTextView:

class CityAutocompleteAdapter(context: Context?)
    : ArrayAdapter<City>(context, R.layout.spinner_city),
        Filterable {

    init {

    }

    private var resultList: List<City> = arrayListOf()

    override fun getCount(): Int {
        return resultList.size
    }


    override fun getItem(position: Int): City = resultList.get(position)

    override fun getItemId(position: Int): Long = position.toLong()


    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        var inflater = LayoutInflater.from(context)


        val city = getItem(position)
        var convertView = inflater.inflate(R.layout.spinner_city, parent, false)

        var name: TextView = convertView.findViewById(R.id.name)
        var country: TextView = convertView.findViewById(R.id.subtitle)
        var flag: ImageView = convertView.findViewById(R.id.flag)

        name.text = city.name

        GlideApp.with(context)
                .load(city.flag)
                .into(flag)

        country.text = city.country


        return convertView
    }

    override fun getFilter(): Filter {
        var filter = object : Filter() {
            override fun performFiltering(constraint: CharSequence?): FilterResults {
                Timber.w("Filtering")
                var filterResults = FilterResults()
                if (constraint != null) {
                    var locations = findCities(constraint.toString())
                    filterResults.values = locations
                    filterResults.count = locations.size
                }
                return filterResults
            }

            override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
                if (results != null && results.count > 0) {
                    resultList = results.values as (List<City>)
                    notifyDataSetChanged()
                } else {
                    notifyDataSetInvalidated()
                }
            }

        }

        return filter
    }

    /**
     * Returns a search result for the given book title.
     */
    private fun findCities(bookTitle: String): List<City> {
        return synchronizer.waitForAPI(bookTitle)
    }

    private val synchronizer = Synchronizer()

    private inner class Synchronizer {
        internal var result: List<City>? = null

        private val lock = java.lang.Object()

        internal fun waitForAPI(constraint: CharSequence): List<City> = synchronized(lock) {
            var repository: CityRepository = CityRepositoryImpl()
            repository.getCitiesByName(constraint.toString())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeBy(onError = {

                    }, onSuccess = {
//                        Timber.w("Results for $constraint are $it")
                        result = it
                        notifyAPIDone(it)
                    })

            // At this point, control returns here, and the network request is in-progress in a different thread.
            try {
                // wait() is a Java IPC technique that will block execution until another
                // thread calls the same object's notify() method.
                lock.wait()
                // When we get here, we know that someone else has just called notify()
                // on this object, and therefore this.result should be set.
            } catch (e: InterruptedException) {
            }

            return this.result ?: emptyList()
        }


        internal fun notifyAPIDone(result: List<City>) = synchronized(lock) {
            this.result = result
            // API result is received on a different thread, via the API callback.
            // notify() will wake up the other calling thread, allowing it to continue
            // execution in the performFiltering() method, as usual.
            lock.notify()
        }
    }

}

As you can see I've put a log line right on the performFiltering line, and what I get is, after I type (and stop typing) this is shown non stop on my logcat:

07-11 13:41:56.469 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:41:57.221 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:41:57.975 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:41:58.728 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:41:59.481 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:00.232 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:00.985 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:01.738 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:02.491 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:03.243 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:03.995 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering

Even when I close the app (onPause) this is being executed! How is this possible? If I have an api call on there, it will keep performing the request non stop until the app is cleared from memory.

EDIT:

Fragment that uses this Adapter:

class CitySearchFragment : BaseStepImportFragment<ImportViewModel>() {
    override fun backButtonPressed() {
        goBack()
    }

    override val viewModelClass: Class<ImportViewModel> = ImportViewModel::class.java
    override val layoutId: Int = R.layout.fragment_import_notes

    override fun inject() {

        var component = (activity as TextImportActivity).component
        component.inject(this)
    }

    override fun loadUp() {
        Timber.e("Saying hello within $this, viewModel $viewModel")
        setHasOptionsMenu(true)
        city_search_combo.threshold = AUTOCOMPLETE_MIN_THRESHOLD
        city_search_combo.setAdapter(CityAutocompleteAdapter(context))
        city_search_combo.setOnItemClickListener { adapterView, view, i, l ->
            val item = adapterView.getItemAtPosition(i) as City
            city_search_combo.setText(item.name)
        }


    }


    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.textcopy_menu, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.text_copy_done -> {
                viewModel.sendText(city_search_combo.text.toString(), notes_text.text.toString())
                city_search_combo.clearFocus()
                notes_text.clearFocus()
                goNext()
            }
        }

        return super.onOptionsItemSelected(item)
    }

    companion object {
        val AUTOCOMPLETE_MIN_THRESHOLD = 3
        fun newInstance() = CitySearchFragment()
    }
}



from AutoCompleteTextView's performFiltering called all the time

No comments:

Post a Comment