简体   繁体   English

Android Kotlin 从带参数的视图中调用 ViewModel function

[英]Android Kotlin Calling ViewModel function from View with Parameters

I am building an Android app.我正在构建一个 Android 应用程序。

I have a layout that contains a button 'saveButton'.我有一个包含按钮“保存按钮”的布局。 When the user clicks the button, the onClick should call a function in my ViewModel, onSave().当用户单击按钮时,onClick 应该在我的 ViewModel 中调用 function onSave()。 This function requires 2 parameters: the text contents of 2 EditText views that are also present in the same layout.这个 function 需要 2 个参数:2 个 EditText 视图的文本内容,它们也出现在同一布局中。

Basically, the user has edited the name and/or the synopsis and now wants to have the ViewModel update the object's data in the database.基本上,用户已经编辑了名称和/或概要,现在希望 ViewModel 更新数据库中对象的数据。

(Part of) my UI (.xml fragment layout): (部分)我的用户界面(.xml 片段布局):

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".ui.CreateEditRelationFragment">

<data>
    <variable
        name="createEditRelationViewModel"
        type="be.pjvandamme.farfiled.viewmodels.CreateEditRelationViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/saveText"
        android:onClick="@{() -> createEditRelationViewModel.onSave(relationNameEditText.getEditText().getText().toString(), relationSynopsisEditText.getEditText().getText().toString())}" />

    <Button
        android:id="@+id/cancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:text="@string/cancelText"
        android:onClick="@{() -> createEditRelationViewModel.onCancel()}" />

    <EditText
        android:id="@+id/relationSynopsisEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:gravity="start|top"
        android:inputType="textMultiLine" />

    <EditText
        android:id="@+id/relationNameEditText"
        android:layout_width="@dimen/relationNameEditWidth"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="16dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />
</androidx.constraintlayout.widget.ConstraintLayout>

(Part of) the ViewModel: (部分)ViewModel:

class CreateEditRelationViewModel (
    private val relationKey: Long?,
    val database: RelationDao,
    application: Application
): AndroidViewModel(application){
private var viewModelJob = Job()

private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
private var relation = MutableLiveData<Relation?>()

private var _navigateToRelationDetail = MutableLiveData<Boolean>()
val navigateToRelationDetail: LiveData<Boolean>
    get() = _navigateToRelationDetail

fun onSave(name: String, synopsis: String){
    Timber.i("Got name: " + name + " and synopsis: " + synopsis)
    if(relationKey == null){
        uiScope.launch{
            relation.value = Relation(0, name, synopsis, false)
            insert(relation.value!!)
        }
    }
    else{
        uiScope.launch{
            relation.value?.name = name
            relation.value?.synopsis = synopsis
            update(relation.value)
        }
    }
    _navigateToRelationDetail.value = true
}

private suspend fun insert(newRelation: Relation){
    withContext(Dispatchers.IO){
        database.insert(newRelation)
    }
}

private suspend fun update(relation: Relation?){
    if(relation != null) {
        withContext(Dispatchers.IO) {
            database.update(relation)
        }
    }
}
}

I would want this thing to compile so that the onSave() function is called and the contents of the EditTexts passed as parameters.我希望编译这个东西,以便调用 onSave() function 并将 EditTexts 的内容作为参数传递。

I cannot manage to pass the text contents of these EditTexts.我无法设法传递这些 EditTexts 的文本内容。 The compiler throws this error:编译器抛出此错误:

[databinding] {"msg":"cannot find method getEditText() in class android.widget.EditText","file":"D:\\ etc.

It does the same thing when I try to access using the.text property directly.当我尝试直接使用 .text 属性访问时,它会做同样的事情。 Does anyone know what I'm doing wrong?有谁知道我做错了什么? I'm tearing my hair out.我在扯头发。

getEditText() method doesn't exist and you can remove it. getEditText()方法不存在,您可以将其删除。

instead of;代替;

relationNameEditText.getEditText().getText().toString()

you can do你可以做

relationNameEditText.getText().toString()` 

ie https://github.com/dgngulcan/droid-feed/blob/e0d0d5f4af07c5375d42b74e42c55b793319a937/app/src/main/res/layout/fragment_newsletter.xml#L120https://github.com/dgngulcan/droid-feed/blob/e0d0d5f4af07c5375d42b74e42c55b793319a937/app/src/main/res/layout/fragment_newsletter.xml#L120

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM