绑定不起作用 - 怎么办?

Binding not working - how to?

我不明白为什么我的 java bean 属性在用户更改某些文本字段中的文本后没有更新。我已经配置了 pojo 和绑定,但它不起作用。有人能解释一下为什么不起作用吗? 如果我使用 setter 在 onCreate 方法中初始化 bean,则绑定只是在开始时起作用。但是当我调用 onSignup 方法时,更改不会合并到用户 bean 中。

activity_registration.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable name="user" type="main.client.android.musichall.registration.model.User" />
    </data>

    <android.support.constraint.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/activity_horizontal_margin"
        tools:context="main.client.android.musichall.registration.RegistrationActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@array/activity_registration_modes" />

    <TextView
        android:id="@+id/activity_registration_text_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/spinner"
        tools:layout_editor_absoluteX="8dp"
        android:text="@string/activity_registration_text_name" />

    <EditText
        android:id="@+id/activity_registration_text_field_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintTop_toBottomOf="@+id/activity_registration_text_name"
        tools:layout_editor_absoluteX="8dp"
        android:text="@{user.name}"/>

    <TextView
        android:id="@+id/activity_registration_text_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="@string/activity_registration_text_email"
        app:layout_constraintTop_toBottomOf="@+id/activity_registration_text_field_name"
        tools:layout_editor_absoluteX="8dp" />

    <EditText
        android:id="@+id/activity_registration_text_field_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:ems="10"
        android:inputType="textEmailAddress"
        app:layout_constraintTop_toBottomOf="@+id/activity_registration_text_email"
        tools:layout_editor_absoluteX="8dp"
        android:text="@{user.email}"/>

    <TextView
        android:id="@+id/activity_registration_text_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="@string/activity_registration_text_password"
        app:layout_constraintTop_toBottomOf="@+id/activity_registration_text_field_email"
        tools:layout_editor_absoluteX="8dp" />

    <EditText
        android:id="@+id/activity_registration_text_field_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPassword"
        android:text="@{user.password}"
        app:layout_constraintTop_toBottomOf="@+id/activity_registration_text_password"
        tools:layout_editor_absoluteX="8dp" />

    <Button
        android:id="@+id/activity_registration_button_sign_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="@string/sign_up"
        android:onClick="onSignUpClick"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    </android.support.constraint.ConstraintLayout>

</layout>

RegistrationActivity.java

package main.client.android.musichall.registration;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import main.client.android.musichall.R;
import main.client.android.musichall.databinding.ActivityRegistrationBinding;
import main.client.android.musichall.login.LoginActivity;
import main.client.android.musichall.registration.model.User;

public class RegistrationActivity extends AppCompatActivity {

    private static RegistrationPresenter presenter;
    private ActivityRegistrationBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        presenter = new RegistrationPresenter(this);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_registration);
        User user = new User();
        binding.setUser(user);

    }

    public void onSignUpClick (View view) {
        User user = binding.getUser();
        user.getEmail();
        presenter.onSignUpClick(user);
    }

    public void onSignUpDone(User user) {
        startActivity(new Intent(this, LoginActivity.class));
    }
}

User.class

package main.client.android.musichall.registration.model;

import android.databinding.BaseObservable;
import android.databinding.Bindable;

import main.client.android.musichall.BR;

/**
 * Created by sangra on 02/12/17.
 */

public class User extends BaseObservable{

    private String name;
    private String username;
    private String email;
    private String password;

    @Bindable
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    @Bindable
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
        notifyPropertyChanged(BR.username);
    }

    @Bindable
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
        notifyPropertyChanged(BR.email);
    }

    @Bindable
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        notifyPropertyChanged(BR.password);
    }
}

您似乎没有实现双向数据绑定。所以目前你的视图只会显示你的用户对象中的内容,不会改变它的值。

要实现双向数据绑定,您需要更改以下内容: android:text="@{user.password}"android:text="@={user.password}"

绑定user.password

时请注意@后的=

当文本现在输入到您的 EditText 中时,应更新用户中的相应字段。

希望这对您有所帮助。

来源:https://www.bignerdranch.com/blog/two-way-data-binding-on-android-observing-your-view-with-xml