【问题标题】:How to pass an argument to a function in data binding?如何将参数传递给数据绑定中的函数?
【发布时间】:2019-09-10 06:04:17
【问题描述】:

我的布局文件中有两个变量:

<data>
  <variable name="createExpenseViewModel" type="com.lionosur.dailyexpenses.viewModels.MainViewModel"/>
  <variable name="createExpenseConverter" type="com.lionosur.dailyexpenses.converters.createExpenseActivityConverter.Companion"/>
</data>

我的视图模型有一个返回实时数据的方法:

fun getAllExpenseItems(): LiveData<List<Expense>> {
    return expenseRepository.getAllExpenseItems()
}

我需要观察这些数据并填充微调器,

class createExpenseActivityConverter {
    // contains all the static methods to convert the data for the ui
    companion object {

        fun getExpenseCategoryListFromSource(list:List<Source>):ArrayList<String> {
            val categoryItems = ArrayList<String>()
            categoryItems.addAll(list.map { it.sourceName })
            return categoryItems
        }
    }
}

要填充微调器,我需要提供一个字符串数组列表

<Spinner
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/expense_category"
        android:entries="@{()-> 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"
/>

在android:entries中,我需要将观察到的数据转换为字符串数组列表,如何将@{()-&gt; createExpenseViewModel.getAllSourceItems(1) } 结果传递给另一个静态方法createExpenseViewConverter.getExpenseCategoryListFromSource(sourceList),它将返回一个数组列表字符串。

在我的活动中,我设置了这样的绑定

binding = DataBindingUtil.setContentView(this, R.layout.activity_create_expense)



val mainViewModel =  DaggerExpenseComponent.builder()
    .setContext(this)
    .build()
    .getExpenseViewModel()

binding.setLifecycleOwner(this)
binding.createExpenseViewModel = mainViewModel

【问题讨论】:

    标签: android kotlin android-databinding


    【解决方案1】:

    您需要使用以下语法:

    android:entries="@{createExpenseConverter.getExpenseCategoryListFromSource(createExpenseViewModel.getAllSourceItems(1))}"
    

    在这里,我们所做的是使用getAllSourceItems() 方法从MainViewModel 对象createExpenseViewModel 访问您的输入;

    然后将它传递给另一个类 createExpenseActivityConverter 对象 createExpenseConverter 使用方法 getExpenseCategoryListFromSource() 返回您的微调器所需的 ArrayList&lt;String&gt;。


    编辑:

    当您在 DataBinding 中使用 LiveData 时,Data-binding Compiler 会像 ObservableFields 一样负责刷新数据。您需要做的就是将您的LifeCycleOwner 提供给您的数据绑定对象。

    例如:

    如果您的活动有 ViewDataBinding 假设 mActivityBinding 使用它提供您的 ViewModel 以在 xml 绑定中设置 LiveData,然后在设置您的 ViewModel 之后考虑设置 LifecycleOwner 像下面的代码:

    //Some Activity having data-binding
    ... onCreate() method of activity
        mActivityBinding.setViewModel(myViewModel);
        mAcivityBinding.setLifecycleOwner(this); // Providing this line will help you observe LiveData changes from ViewModel in data-binding.
    ...
    

    参考here

    【讨论】:

    • createExpenseViewModel.getAllSourceItems(1) 返回实时数据,我不需要观察吗?
    • 不,它将由数据绑定编译器在内部处理。
    • 因为它返回 null 并且没有为我填充微调器,所以我使用了你的答案。
    • 即使您想观察实时数据,也只需在您各自的活动/片段中使用这一行:binding.setLifecycleOwner(this) 在您的绑定对象上 onCreate() 设置所有变量后。
    • 问题是我想知道在数据绑定中观察实时数据的语法是什么,并将观察到的值用于转换器
    猜你喜欢
    • 2022-01-19
    • 2021-02-14
    • 1970-01-01
    • 2022-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    相关资源
    最近更新 更多