2

I try to use AutoCompleteTextView with ArrayAdapter. everything works correctly before first try to use AutoCompleteTextView. after the first try, just AutoCompleteTextView shows the last selection (and in this case your first try with AutoCompleteView). I think with clear() and notifyDataSetChanged() must be ok but after clear() and notifyDataSetChanged() still I have problem. I search for the same question and everything was same but still have problem

this is my adapter:

class CityAutoCompleteAdapter(
    context: Context,
    cityList: ArrayList<CityModel>
) : ArrayAdapter<CityModel>(context, 0, cityList) {

    var cityListFull: ArrayList<CityModel> = cityList

    private var cityFilter: Filter = object : Filter() {
        override fun performFiltering(constraint: CharSequence?): FilterResults {

            val results = FilterResults()
            val suggestions: ArrayList<CityModel> = arrayListOf()

            if (constraint == null || constraint.isEmpty()) {
                suggestions.addAll(cityListFull)
            } else {
                val filterPattern: String = constraint.toString().toLowerCase().trim()

                for (item in cityListFull) {
                    if (item.cityName.toLowerCase().contains(filterPattern)) {
                        suggestions.add(item)
                    }
                }
            }
            results.values = suggestions
            results.count = suggestions.size

            return results
        }

        override fun publishResults(constraint: CharSequence?, results: FilterResults?) {


            if (results != null) {
                //@Suppress("UNCHECKED_CAST")
                addAll(results.values as? List<CityModel> ?: cityListFull)
                notifyDataSetChanged()
            }

        }

        override fun convertResultToString(resultValue: Any?): CharSequence {
            val result = resultValue as CityModel
            return result.cityName
        }
    }

    override fun getFilter(): Filter {
        return cityFilter
    }


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

        var newConvertView = convertView

        if (convertView == null) {
            newConvertView = LayoutInflater.from(context).inflate(
                R.layout.simple_auto_complete_item, parent, false
            )
        }

        val txtAutoCompleteCityName: TextView =
            newConvertView?.findViewById(R.id.txtAutoCompleteLabel) ?: TextView(context)

        val cityItem: CityModel? = getItem(position)

        if (cityItem != null) {
            txtAutoCompleteCityName.text = cityItem.cityName
        }

        return newConvertView!!
    }
}

and this is my class where I used AutoCompleteView:

 cityAdapter = result.success()?.let {
                    CityAutoCompleteAdapter(
                        requireContext(),
                        it
                    )
                }

                binding.layoutFragmentPickupSender.layoutBottomSheetSender.autoAddSenderStateCity.setAdapter(cityAdapter)
                cityAdapter?.notifyDataSetChanged()


    binding.layoutFragmentPickupSender.layoutBottomSheetSender.autoAddSenderStateCity.onItemClickListener =
            OnItemClickListener { adapterView, view, pos, id -> //this is the way to find selected object/item
                selectedCity = adapterView.getItemAtPosition(pos) as CityModel
                cityAdapter?.notifyDataSetChanged()
            }

may please guide me where I make a mistake?

Mehrdad Dolatkhah
  • 668
  • 2
  • 9
  • 28
  • I have tried. Its' hard to recurrent your code. Can you make it simple and runnable or make a repo? – Chuanhang.gu Jun 07 '20 at 08:12
  • 1
    I try to implement this adapter for AutoCompleteTextView `https://codinginflow.com/tutorials/android/custom-autocompletetextview/part-2-adapter` with kotlin. may please guide me – Mehrdad Dolatkhah Jun 07 '20 at 09:11
  • Why not try android studio convert java to kotlin util? – Chuanhang.gu Jun 07 '20 at 09:49
  • 1
    when try to convert to kotlin in android studio adapter code I got a problem in this section: `addAll(results.values as List<*>)` with this error: `following functions can be called with the arguments supplied: public open fun addAll(p0: (MutableCollection..Collection)): Unit defined in com.chaparnet.chapar.views.pickup.CityAutoCompleteAdapter public open fun addAll(vararg p0: CityModel?): Unit defined in com.chaparnet.chapar.views.pickup.CityAutoCompleteAdapter` – Mehrdad Dolatkhah Jun 07 '20 at 09:56
  • You refered post, code is not complete. How about modify it to addAll(results.values as MutableList) – Chuanhang.gu Jun 07 '20 at 10:00
  • yes, I try this one and after this, I have this warning: ` addAll(results.values as MutableList` but with this convert to Kotlin I have the same issue. the first time everything works perfectly as I except, in the second time I have just last Item selected and in the third time I have nothing. I don't know where but I guess somewhere probably after first run I delete list in the adapter. according to the breakpoint in the second time, cityListFull is empty as I check. – Mehrdad Dolatkhah Jun 07 '20 at 10:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215461/discussion-between-mehrdad-dolatkhah-and-chuanhang-gu). – Mehrdad Dolatkhah Jun 07 '20 at 10:17

0 Answers0