I have a autocomplete textview where what I am doing is , after entering at least three character a api will call and I am getting a list from the api which I am using as a suggestion but the problem is after selecting a option the suggestion pop is comming again and again , how can I stop that?
binding.etSpecialization.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable?) {
Log.d(Constants.DoctorTag, "inside specialities funtion")
if (!s.isNullOrBlank()){
try {
val specialities=s.toString()
Log.d(Constants.DoctorTag, "value send--$specialities")
specializationViewModel.getSpecialities(specialities)
}catch (e: Exception) {
// Handle the case where the input is not a valid integer
Log.e(Constants.DoctorTag, "Invalid pincode: $e")
}
}
}
})
here I am calling the api and
specializationViewModel.specialities.observe(this){
when(it){
is NetworkResponseHandler.Success ->{
val SpecialitiesList=it.data?.data
Log.d(Constants.DoctorTag, "This is specialities list ------${SpecialitiesList}")
Log.d(Constants.DoctorTag, "This is specialities total list ------${it.data?.total}")
try {
if (SpecialitiesList!=null){
Log.d(Constants.DoctorTag, "list is not null")
specialitiesAdapter= SpecialitiesAdapter(this,SpecialitiesList)
binding.etSpecialization.setAdapter(specialitiesAdapter)
binding.etSpecialization.showDropDown()
binding.etSpecialization.threshold=1
binding.etSpecialization.setOnItemClickListener { _, _, position, _ ->
val selcetedSpecialities=specialitiesAdapter.getItem(position)
if (selcetedSpecialities!=null){
val specialities=selcetedSpecialities.title
binding.etSpecialization.setText(specialities)
specialtyId=selcetedSpecialities.id
binding.etSpecialization.clearFocus()
binding.etSpecialization.dismissDropDown()
}
else {
Toast.makeText(this
, "null value ", Toast.LENGTH_LONG).show()
binding.etSpecialization.clearFocus()
}
}
}
else{
Log.d(Constants.DoctorTag, "Specialities list is null------")
Toast.makeText(this,"Specialities not found",Toast.LENGTH_LONG).show()
}
}catch (e:Exception){
Log.d(Constants.DoctorTag, "specialities list exception---$e")
}
}
is NetworkResponseHandler.Error ->{
Log.d(Constants.DoctorTag, "pincodes list ${it.massage}")
}
is NetworkResponseHandler.Loading ->{
Log.d(Constants.DoctorTag, "pincodes list loading ${it.massage}")
}
}
}
here I am handling the response , I haved used the clearfocus() and dismissdropdown also but nothing is working
afterTextChanged method calling viewmodel method and whenever the call success you changing the "binding.etSpecialization" so code enters the afterTextChanged method again. That is why are you getting suggestion pop up again and again.
You should try not to call the afterTextChanged method again or avoid calling the service again when the code enters the afterTextChanged method.
May be you can use
binding.etSpecialization.removeTextChangedListener()on first line ofis NetworkResponseHandler.Success ->{and use on the last lines of your success casebinding.etSpecialization.addTextChangedListener()