[英]Android data binding two-way not working
I am trying to use two-way data binding for editing user data. 我正在尝试使用双向数据绑定来编辑用户数据。 I am able to show the POJO contents in the view but the changes made by user are not able to capture back with POJO.
我能够在视图中显示POJO内容,但用户所做的更改无法使用POJO捕获。
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User();
user.setFirstName("first a");
user.setLastName("last b");
binding.setUser(user);
}
public void Click(View v){
//ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
User user = binding.getUser();
Toast.makeText(this,user.getFirstName(),Toast.LENGTH_LONG).show();
}
}
I have tried using a global variable for user
, binding
too. 我已经尝试过为
user
使用全局变量, binding
也是如此。 But when I click the button, it always shows the "first a". 但是当我点击按钮时,它总是显示“第一个”。 The definition for User.java is:
User.java的定义是:
public class User extends BaseObservable{
private String firstName;
private String lastName;
@Bindable
public String getFirstName(){ return firstName;}
public void setFirstName(String first) {
firstName = first;
notifyPropertyChanged(com.example.ks.myapplication.BR.firstName);
}
@Bindable
public String getLastName(){ return lastName;}
public void setLastName(String last) {
lastName = last;
notifyPropertyChanged(com.example.ks.myapplication.BR.lastName);
}
}
and the XML: Here, I am trying to display the changes immediately in text views and on button click also. 和XML:在这里,我试图立即在文本视图和按钮点击中显示更改。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.example.ks.myapplication.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:text="@{user.firstName}" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="@{user.lastName}" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:onClick="Click" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName}"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.lastName}"/>
</LinearLayout>
</layout>
Is there anything I am missing? 有什么我想念的吗?
Two way data binding facility is still under implementation. 双向数据绑定设施仍在实施中。
You have upgrade project to latest alpha build (com.android.tools.build:gradle:2.1.0-alpha3) and have to use @={..}
type expression!! 你有升级项目到最新的alpha版本(com.android.tools.build:gradle:2.1.0-alpha3),并且必须使用
@={..}
类型表达式!!
See details at: https://halfthought.wordpress.com/2016/03/23/2-way-data-binding-on-android/ 详见: https : //halfthought.wordpress.com/2016/03/23/2-way-data-binding-on-android/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.