如何为Spinner编写一个绑定适配器,以接收LiveData作为输入? [英] how to write a binding adapter for spinner which receives LiveData as input?
问题描述
我在xml中定义了一个微调框,
i have a spinner defined in the xml like this
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/expense_category"
app:sourceData="@{()->createExpenseViewModel.getAllSourceItems(1)}"
app:layout_constraintStart_toStartOf="@+id/textView"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintWidth_percent="0.7"
/>
createExpenseViewModel.getAllSourceItems(1)此方法返回LiveData< List< Source >>,因此我已经为这种情况编写了绑定适配器
createExpenseViewModel.getAllSourceItems(1) this method returns LiveData <List<Source>>, so i have written a binding adapter for that case
@BindingAdapter("app:sourceData")
fun setSourceData(spinner: Spinner, sourceList: List<Source>) {
val categoryItems = ArrayList<String>()
categoryItems.addAll(sourceList.map { it.sourceName })
val spinnerAdapter =
ArrayAdapter<String>(spinner.context, R.layout.simple_spinner_dropdown_item, categoryItems)
spinner.adapter = spinnerAdapter
}
在构建应用程序时,出现以下错误,
****/ data binding error ****msg:Cannot find the proper callback class for app:sourceData. Tried java.util.List but it has 25 abstract methods, should have 1 abstract methods. file:/home/naveen/Desktop/project-expense/app/src/main/res/layout/activity_create_expense.xml loc:94:34 - 94:80 ****\ data binding error **
**
when building the app, i am getting the following error,
****/ data binding error ****msg:Cannot find the proper callback class for app:sourceData. Tried java.util.List but it has 25 abstract methods, should have 1 abstract methods. file:/home/naveen/Desktop/project-expense/app/src/main/res/layout/activity_create_expense.xml loc:94:34 - 94:80 ****\ data binding error **
**
此错误实际上是什么意思,如何解决此错误?
what does this error actually mean,how to resolve this error?
我打算做的是获取实时数据返回的列表并将其转换为ArrayList类型,一旦实时数据返回列表,我就需要触发绑定适配器,但是如果我使用此应用程序:sourceData ="@ {createExpenseViewModel .getAllSourceItems(1)}"并设置绑定适配器,该适配器仅获取空列表
what i intend to do is get the list returned by live data and convert to type ArrayList , i need my binding adapter to be triggered once the livedata returns the list, but if i use this app:sourceData="@{createExpenseViewModel.getAllSourceItems(1)}" and set the binding adapter, the adapter get only null list
推荐答案
我遵循了@muetzenflo建议的核心思想,我在这样的视图模型上创建了一个属性
I have followed the core idea of what @muetzenflo suggested, i have created a property on view model like this
class MainViewModel @Inject constructor(
val expenseSourceItems:LiveData<List<Source>> = getAllSourceItems(1)
fun getAllSourceItems(sourceType:Int?): LiveData<List<Source>> {
val res = sourceRepository.getAllSourceItems(sourceType)
return res
}
// the methods below are omitted for brevity
}
然后我已经使用属性访问语法绑定到微调器
then i have bound to the spinner using property access syntax
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/expense_category"
app:sourceData="@{createExpenseViewModel.expenseSourceItems}"
app:layout_constraintStart_toStartOf="@+id/textView"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintWidth_percent="0.7"
/>
,然后使用相同的绑定适配器
and then used the same binding adapter
@BindingAdapter("app:sourceData")
fun setSourceData(spinner: Spinner, sourceList: List<Source>) {
val categoryItems = ArrayList<String>()
categoryItems.addAll(sourceList.map { it.sourceName })
val spinnerAdapter =
ArrayAdapter<String>(spinner.context, R.layout.simple_spinner_dropdown_item, categoryItems)
spinner.adapter = spinnerAdapter
}
对于实时数据,调用数据绑定内部的方法仅适用于诸如onclick之类的回调,并且需要将属性访问用于正常的数据绑定(如填充微调器).
for live data calling a method inside data binding only works for callbacks like onclick, and property access is needed to be used for normal data binding like populating a spinner.
这篇关于如何为Spinner编写一个绑定适配器,以接收LiveData作为输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!