Reputation: 1641
I've gone through many post related to the data-binding but didn't find the solution for my problem. I've created a sample app to learn the data binding.
Expected Behaviour: I've a edittext and textview. The textview should get updated with whatever I write in the edittext.
Problem: I've created a Observable and link it to both edittext and textview. While writing inside the edittext, my textview is not updating. I'm doing something wrong here. Please check the code below-
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
activityMainBinding.setStudent(new Student("Rahul"));
activityMainBinding.executePendingBindings();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="student"
type="com.rahulchaurasia.databindingtest.Student"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.rahulchaurasia.databindingtest.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@{student.name}"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{student.name}"/>
</LinearLayout>
</layout>
Student.java
public class Student {
public ObservableField<String> name;
public Student(String n) {
name = new ObservableField<>(n);
}
}
Please help me.
Upvotes: 2
Views: 1050
Reputation: 7257
The binding of the EditText
should be bidirectional @={student.name}
.
Upvotes: 7