2017-10-05 3 views
-1

Mein Problem ist die binding ist Arbeit, aber nicht korrekt! Wenn ich neuen Text in textfield eintippe und dann versuche, Daten von diesem textfield mit Hilfe binding zu erhalten, sehe ich alte Daten. Ich habe versucht, den Fehler zu finden, aber ich konnte nicht.Android-Kotlin-Bindung aktualisiert nicht die Eigenschaften

Mein activity_fblogin.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> 
    </data> 

    <android.support.constraint.ConstraintLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/grey_main" 
     android:descendantFocusability="beforeDescendants" 
     android:fitsSystemWindows="true" 
     android:focusableInTouchMode="true" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     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="test01" 
      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="123456" 
      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/loginButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginEnd="8dp" 
      android:layout_marginStart="8dp" 
      android:layout_marginTop="8dp" 
      android:text="Login" 
      android:textColor="@color/green_main" 
      app:layout_constraintEnd_toEndOf="@+id/userPass" 
      app:layout_constraintTop_toBottomOf="@+id/userPass" /> 

    </android.support.constraint.ConstraintLayout> 

</layout> 

Es ist meine kotlin Klasse für fblogin_activity. Ich habe versucht, dieses Problem zu beheben, aber ... Gib mir bitte eine Idee!

package com.example.darkt.makeyouself.activities 

class FBLogin : AppCompatActivity() { 

    private var binding: ActivityFbloginBinding? = null 
    private var auth: FirebaseAuth? = null 
    private var dbHelper: FirebaseHelper? =null 

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

     binding?.executePendingBindings() 

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

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

     Toast.makeText(this, email, Toast.LENGTH_SHORT).show() 
     Toast.makeText(this, password, Toast.LENGTH_SHORT).show() 
    } 
} 

Example mistake

Antwort

0

Es gibt nichts in Ihrem Layout xml, die tatsächlich verwendet Datenbindung.

In Ihrem Tag müssen Sie zuerst ein Objekt für die Datenbindung definieren. zum Beispiel:

<data> 
    <variable name="user" type="com.myapp.User"/> 
</data> 

weiter unten im Benutzername/Passwort-Feld dieser Benutzer für das Textfeld

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


    <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"/> 

Beachten Sie die @ = {} user.username im Textfeld verweisen müsste. Für jede Eigenschaft des Klassenbenutzers müssen Sie einen Setter/Getter angeben. Das @ = bedeutet, das zugrunde liegende Objekt zu aktualisieren, wenn sich dieser Wert ändert.

vor Ihrem „executePendingBindings“ Sie müssen so etwas wie dies tun:

val user = User() 
binding.setVariable(BR.user, user) 
binding.executePendingBindings() 

dann sollten Sie gut zu gehen.

+0

Vielen Dank für Ihre Antwort, Chris! Aber es funktioniert nicht. Vor 20 Tagen habe ich eine Frage gestellt: https://stackoverflow.com/questions/46380992/kotlin-android-binding-not-update-data niemand beantwortete die Frage –

Verwandte Themen