双向数据绑定与EditText

我正在使用Kotlin开发一个简单的Android应用程序。 我想使用数据绑定,但我不能让他们与EditText工作

这是我的XML布局

 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <import type="de.knerd.applicationmanager.models.AgencyModel"/> <variable name="agency" type="AgencyModel"/> </data> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_add_agency" tools:context="de.knerd.applicationmanager.activities.AddAgencyActivity" android:orientation="vertical" android:layout_margin="16dp"> <android.support.design.widget.TextInputLayout android:layout_height="match_parent" android:layout_width="match_parent"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/name" android:labelFor="@+id/name" android:hint="@string/agency_name" android:text="@={agency.name}"/> </android.support.design.widget.TextInputLayout> </RelativeLayout> </layout> 

这是我的Model类

 package de.knerd.applicationmanager.models import android.databinding.BaseObservable import android.databinding.Bindable import com.j256.ormlite.dao.ForeignCollection import com.j256.ormlite.field.DatabaseField import com.j256.ormlite.field.ForeignCollectionField import com.j256.ormlite.table.DatabaseTable import de.knerd.applicationmanager.BR @DatabaseTable(tableName = "agency") class AgencyModel : BaseObservable() { @DatabaseField(unique = true, canBeNull = false) @get:Bindable @set:Bindable var name: String? = null set(value) { field = value notifyPropertyChanged(BR.name) } @DatabaseField(generatedId = true) @get:Bindable var id: Int = 0 @ForeignCollectionField(eager = false) @get:Bindable var agents: ForeignCollection<AgentModel>? = null } 

这是将模型与视图绑定的部分

 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView<ContentAddAgencyBinding>(this, R.layout.content_add_agency) binding.agency = AgencyModel() setContentView(R.layout.activity_add_agency) val toolbar = findViewById(R.id.toolbar) as Toolbar setSupportActionBar(toolbar) supportActionBar!!.setDisplayHomeAsUpEnabled(true) } 

当我尝试访问绑定中的模型的属性时,我得到一个空值。 我使用的代码是这样的

 private fun save(): Boolean { try { val agency = binding.agency Log.d("Name", agency.name) return true } catch (ex: Exception) { return false } } 

你并没有像Knerd那样使用Binding。 双向绑定旨在让您可以访问代码中的对象或字段,而无需引用UI代码。 通过你做binding.agency你是没有不同的findViewById()你打败了绑定的目的。

首先解决你的绑定问题。 使用BaseObservable作为你的对象模型,它应该保持最新的屏幕上的任何值的变化。

然后当你保存它应该没有什么比访问本地变量。

 private fun save(): Boolean { try { Log.d("Name", localObservableAgency.name) return true } catch (ex: Exception) { return false } } 

//不知道这个方法是什么,猜测你试图捕获null。 更好的方法是只对对象执行空检查,因为通过访问空值来捕获异常很重,并且不会增加值。 此外,Kotlin是无效的安全语言,所以你要么失去了绑定的价值,可能是将它设置为null,或者在这里奇怪地编码了某些东西,因为如果你保留一个本地副本,你必须故意使一个对象为空。 然而,你没有保留一个本地副本,你只是新建一个内联,通过绑定后访问,就好像它是你的变量存储库哈哈。

简而言之,就是使用数据绑定的方式来使用它,这样你就不会有问题了。 这可能是为什么有人投票给你是我的猜测。

我发现问题,这是由我绑定到错误的布局造成的。 正确的代码如下。

 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView<ActivityAddAgencyBinding>(this, R.layout.activity_add_agency) binding.agency = AgencyModel() val toolbar = findViewById(R.id.toolbar) as Toolbar setSupportActionBar(toolbar) supportActionBar!!.setDisplayHomeAsUpEnabled(true) } 

在这两种情况下,我必须绑定到活动布局