[JAVA] (Android) Dynamically change text using DataBinding

Introduction

Last time, I introduced Try to display static text using DataBinding. This time, I will modify this a little and implement an application that switches the text when the button is clicked.

Define an interface to change the data after clicking

SampleEventHandlers.java


public interface SampleEventHandlers {
    void onChangeClick(View view);
}

Add button to layout/associate with click event

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Binding Objects -->
    <data>
        <variable name="user" type="com.example.databinding2.User" />
        <!--Named handlers(Any)The handler (interface) is set with-->
        <variable name="handlers" type="com.example.databinding2.SampleEventHandlers" />
    </data>

    <!-- View -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.name}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.email}" />

        <Button android:id="@+id/button_change"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Change"
            android:onClick="@{handlers.onChangeClick}" />
            <!--Buttons and SampleEventHandlers.Link onChangeClick of java-->
    </LinearLayout>
</layout>

Added a method to change the value in the model class (the reason for incompleteness will be described later)

User.java


public class User {

    private String name;
    private String email;

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public String getName() { return name; }

    public String getEmail() { return email; }

    //Add setter
    public void setName(String name) {
        this.name = name;
    }

    //Add setter
    public void setEmail(String email) {
        this.email = email;
    }
}

Added DataBinding process (click event)

MainActivity.java


public class MainActivity extends AppCompatActivity implements SampleEventHandlers{

    private User user = new User("Taro", "[email protected]");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Get an instance of Binding
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        //Associate xml user with MainActivity user
        binding.setUser(user);
        //OnChangeClick of MainActivity in handlers of xml()Link
        binding.setHandlers(this);
    }

    //Event handling when button is clicked
    @Override
    public void onChangeClick(View view) {
        if (user.getName().equals("Taro")) {
            user.setName("Jiro");
            user.setEmail("[email protected]");
            Log.d("DEBUG", user.getName());
        } else {
            user.setName("Taro");
            user.setEmail("[email protected]");
        }
    }
}

I'll try building the app. .. ..

When I do this, the log shows "Jiro", but the characters that appear on the screen remain "Taro". why?

Apparently, it seems that a mechanism to notify View of property changes is necessary, and it is necessary to modify the User model so that objects can be monitored.

Modify model class (notify View of property changes)

Point

User.java


public class User extends BaseObservable {

    private String name;
    private String email;

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

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

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

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

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

Reference site

https://qiita.com/Omoti/items/a83910a990e64f4dbdf1#step0-%E5%B0%8E%E5%85%A5 Thank you very much

Recommended Posts

(Android) Dynamically change text using DataBinding
(Android) Try to display static text using DataBinding
Translator using Microsoft Translator Text API on Android ~ Implementation ~
[Android / Java] Learned about DataBinding