Kotlin android. binding not update data

2020-05-06 04:51发布

问题:

Good evening! I try to use binding in kotlin, but data aren't update. All compile and work, but when user change text and click bsave -> i try to return data from textEdit, and data is not updated. TextEdit contains old data. In java it is working, but in kotlin i have problem. Can you help me?

It is my method onCreate, here i create model and binding its.

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this,R.layout.activity_fblogin)
    auth = FirebaseAuth.getInstance()
    setContentView(R.layout.activity_fblogin)

    user = User("Test02", "123456")
    binding?.setVariable(BR.user, user)
    binding?.executePendingBindings()

    loginButton.setOnClickListener{_ -> loginToSystem()}
    signIn.setOnClickListener{_ -> showSignInActivity()}
}

private fun loginToSystem() {
    binding?.executePendingBindings()
    //showProgressDialog()
    val email = binding?.userName?.text.toString().trim()
    val password = binding?.userPass?.text.toString()

    val email2 = user?.login
    val password2 = user?.password

    if (!checkValidateForm(email, password)) {
        return
    }

It is main part of my 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">

    <data>
        <variable
            name="user"
            type="com.example.darkt.makeyouself.models.User"
        />
    </data>

    <android.support.constraint.ConstraintLayout
        android:descendantFocusability="beforeDescendants"
        android:fitsSystemWindows="true"
        android:focusableInTouchMode="true"
        tools:context="com.example.darkt.makeyouself.activities.FBLogin"
        tools:ignore="MissingConstraints">

        <EditText
            android:id="@+id/userName"
            android:layout_width="220dp"
            android:layout_height="43dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="@{user.login}"
            app:layout_constraintBottom_toBottomOf="@+id/author"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Login"/>


        <EditText
            android:id="@+id/userPass"
            android:layout_width="220dp"
            android:layout_height="45dp"
            android:layout_marginTop="25dp"
            android:ems="10"
            android:inputType="textPassword"
            android:text="@{user.password}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/userName"
            tools:layout_editor_absoluteX="74dp"
            tools:layout_editor_absoluteY="245dp"
            tools:text="Password"/>


        <Button
            android:id="@+id/signIn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@color/green_main"
            android:text="SignIn"
            android:textColor="@color/white"
            app:layout_constraintStart_toStartOf="@+id/userPass"
            app:layout_constraintTop_toBottomOf="@+id/userPass" />


    </android.support.constraint.ConstraintLayout>

</layout>

And my short model:

data class User (val login: String, val password: String)

How i can refresh data in binding after user changes?

回答1:

I was find error! My mistake is duplicating setContentView:

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

When i was delete the second setContentView, i fixed my problem