簡體   English   中英

觀察者模式的個人適配器錯誤

[英]a personal adapter error with observer pattern

我的個人適配器有問題。 當我通過觀察者模式在本地數據庫中搜索信息時,它給我帶來了記錄,但在您第二次嘗試刪除並添加相同的 petra 以進行搜索之前,不會調用 adaptare。 如果我更改字母,我必須將其刪除並再次重新輸入,以便它顯示您在圖像中看到的幫助。

如果你能幫助我,我將不勝感激。

第一次搜索

在此處輸入圖像描述

第二次搜索。 我把字母刪了再輸入

在此處輸入圖像描述

如您所見,現在它可以正常工作了。

我不明白為什么。

Function 呼叫觀察者:

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>))
        })
    }

模型:

@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{
            }
        }
    }
}

適配器:

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 適配器:

<?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 自動完成文本視圖:

<?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>

預計當我第一次輸入一個字母時,如果我刪除並再次添加相同的字母或者輸入兩個或更多字母,它會顯示確實顯示的搜索幫助。

最后,我發現了我的錯誤。 發生的事情是組件 AutoCompleteTextView 已經執行了與 addTextChangedListener 執行的相同的 function,因此它搜索了兩側的信息。 最后,解決方案是刪除 addTextChangedListener,並將完整列表傳遞給適配器,因為它是自動完成的,負責執行它在數據庫中所做的搜索。

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

謝謝您的幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM