0

I have a problem with my personal adapter. When I search through the Observer pattern for information in a local database, it brings me the records but the adaptare is not called until you make a second attempt to delete and add the same petra to search. If I change the letter I have to delete it and re-enter it a second time so that it shows the help as you see in the image.

If you could help me I would appreciate it.

First search

enter image description here

second search. I delete the letter and enter it again

enter image description here

as you can see, now it works correctly.

I don't understood why.

Function call observer:

fun searchCountry(cadena:String){
        var chain_1 = ""
        if(cadena.trim().isNullOrEmpty()){
            chain_1 = ""
        }else{
            chain_1 = cadena.trim() + "%"
        }
        locationViewModel.locationSearch(chain_1)
        locationViewModel.locations.observe(this, Observer { locationList ->
            autoCompleteTextView.setAdapter(LocationAdapter(this,locationList as List<Location>))
        })
    }

VieModel:

@HiltViewModel
class LocationViewModel @Inject constructor(private val getLocationUserCase: GetLocationUseCase) : ViewModel() {
    val locations = MutableLiveData<List<Location?>>()
    fun locationSearch(chain:String){
        viewModelScope.launch {
            val locationLst: List<Location> = getLocationUserCase(chain)
            if(!locationLst?.isNullOrEmpty()){
                locations.postValue(locationLst)
            }else{
            }
        }
    }
}

Adapter:

class LocationAdapter(context: Context, locations: List<Location>) : ArrayAdapter<Location>(context, 0, locations) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.account_frg_user_create_location_items, parent, false)
        getItem(position)?.let { location ->
            view.findViewById<TextView>(R.id.txtCountry).apply {
                text = location.nom_municipio
            }
           view.findViewById<TextView>(R.id.txtProvince).apply {
                text = location.nom_provincia
            }
        }
        return view
    }
}

XML-adapter:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:maxHeight="56dp"
    android:padding="16dp"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtCountry"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Body1"
        android:layout_height="wrap_content"
        tools:text="Pueblo" />

    <TextView
        android:id="@+id/txtProvince"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Body2"
        android:gravity="end"
        android:layout_height="wrap_content"
        tools:text="Provincia" />
</LinearLayout>

XML-Autocomepletetextview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:completionThreshold="1"
        android:imeOptions="actionDone"
        android:inputType="text"
        />
</LinearLayout>

It is expected that when I enter a letter for the first time, it will show the search help that does show if I delete and add the same letter again or enter two letters or more.

David
  • 1
  • 1
  • What happens if you just keep typing after the first letter (without deleting anything)? Are there any results? – FiddlingAway Jan 05 '23 at 19:29
  • Hello, if I don't delete the letter and add another it works correctly. And if, for example, I add the letter "a", the list does not appear, but if I put it back after deleting it, the list appears correctly. – David Jan 05 '23 at 22:08
  • Are there any network requests going through to the database, when you type in the first character? If not, that might mean that your `chain_1` is not recognized as a non-empty string. Specifically this - `cadena.trim().isNullOrEmpty()` seems to be true on the first character. Can you add a very short delay before calling that `if(...)`? For example, 100ms. – FiddlingAway Jan 06 '23 at 08:21
  • hello again. If I have checked and it always sends a character to search for the database and always receives info, and in the cases that it does not work even though it enters the adapter it seems that it does not draw it. Thank you! – David Jan 06 '23 at 17:29

1 Answers1

0

Finally, I found my mistake. What happened is that the component, AutoCompleteTextView, already performed the same function that it was doing with the addTextChangedListener, so it searched for the information on both sides. Finally, the solution is to remove the addTextChangedListener, and pass the complete list to the adapter, since it is the autocomplete that is in charge of carrying out the search that it did on the database.

fun iniciacilarBd(){
    locationViewModel.locationSearch("%")
    // <!--  BASE_DE_DATOS
    locationViewModel.locations.observe(this, Observer { locationList ->
        locations = locationList
        LocationAdapter(this,locations).also { locationAdapter ->
            autoCompleteTextView.setAdapter(locationAdapter)
        }
    })
}

Thanks for the help

David
  • 1
  • 1