2016-08-11 21 views
0

我在我的android应用程序中使用数据绑定,我正在实现双向数据绑定。 我创建使用注释@BindingAdapter这样的自定义属性,找不到符号变量bound_observable

@BindingAdapter({"app:bindingText"}) 
public static void bindEditText(EditText view, final BindableString bindableString) { 

    Pair<BindableString, SimpleTextWatcher> pair = (Pair) view.getTag(R.id.bound_observable); 
    if (pair == null || pair.first != bindableString) { 
     if (pair != null) { 
      view.removeTextChangedListener(pair.second); 
     } 
     SimpleTextWatcher watcher = new SimpleTextWatcher() { 

      @Override 
      public void onTextChanged(String newValue) { 
       bindableString.set(newValue); 
      } 
     }; 
     view.setTag(R.id.bound_observable, new Pair<>(bindableString, watcher)); 
     view.addTextChangedListener(watcher); 
    } 
    String newValue = bindableString.get(); 
    if (!view.getText().toString().equals(newValue)) { 
     view.setText(newValue); 
    } 
} 

它说找不到符号变量bound_observable,我是一种新的数据绑定,所以帮我。

+0

安置自己的XML布局代码显示'bound_observable'结合。 – iRuth

回答

1

您是否在资源中声明R.id.bound_observable

<resources> 
    <item name="R.id.bound_observable" type="id" /> 
</resources> 

任何方式,数据绑定库提供的新版本拥有两路像这样

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