无法更新数据绑定中的片段中的文本

时间:2017-11-22 07:25:43

标签: android data-binding

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">    
    <data>
 <import type="com.demo.Model" />
 <variable
            name="model"
            type="Model" />
</data>
  <Textview
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"                                                          
    android:backgroundTint="@color/colorLableBg"
    android:ems="10"                                                                           
    android:text="@={model.name}"
    android:textCursorDrawable="@null/>
</layout>

这是片段

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_authentication, container, false);
    Model model = getModel()// get model return model data from server
    binding.setModel(model);
    binding.text.setText("This is demo")
    }

我从 api 调用中获取数据并设置模型。以防万一,但是当我设置文本时需要更改。

2 个答案:

答案 0 :(得分:2)

您需要在设置模型后设置该值,否则值将被覆盖。

 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_authentication, container, false);
        Model model = getModel()// get model return model data from server

        binding.setModel(model);
        binding.getModel.setName("this is demo");

        }

答案 1 :(得分:0)

你可以尝试一下:

Model model = getModel()// get model return model data from server
binding.setModel(model);
model.name.set("This is demo")
在您的模型中

确保名称为:

public ObservableField<String> name = new ObservableField<>();