KingFish
KingFish

Reputation: 9153

ObservableField<String> Not Updating?

I am trying to modify my app to use MVVM. Right now, I'm trying to create a simple "password reset" page for which I am trying to get the value of a text field: An email address field. Unfortunately my email field is still coming out null. The viewmodel works as I can properly access onResetPassword.

Any help would be appreciated.

ForgotPasswordViewModel.java

public class ForgotPasswordViewModel extends ViewModel {

    public final ObservableField<String> email = new ObservableField<>();

    public void onResetPassword() {
        Log.i("PASSWORD", "xxx -> " + email.get());
    }
}

activity_forgot_password.xml

<data>
    <variable
        name="viewModel"
        type="com.example.foo.viewModels.ForgotPasswordViewModel" />
</data>

<RelativeLayout
    android:id="@+id/loginLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.LoginActivity">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="15dp">


                <EditText
android:inputType="textEmailAddress|textNoSuggestions"
                    android:text="@{viewModel.email}"
                    android:hint="@string/email" />
                <Button
                    android:text="Login"
                    android:onClick="@{(v) -> viewModel.onResetPassword()}" />
            </LinearLayout>
    </RelativeLayout>
</layout>

Upvotes: 1

Views: 459

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006584

If you are looking to get input from the EditText into the email field, you need to use two-way binding... which means you need an =:

android:text="@={viewModel.email}"

Right now, you are using one-way binding, which fills in the EditText with the contents of email, but does not update email with any user changes.

Upvotes: 2

Related Questions