如何在setModel之后使用DataBinding使模型所做的更改生效?

时间:2018-03-02 09:21:19

标签: android mvvm data-binding binding datamodel

DataBinding:如何确保修改数据模型后视图会相应更新? 例如:

public class MyActivity extends AppCompatActivity {
    private MyActivityBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      mBinding = DataBindingUtil.setContentView(this, R.layout.my_activity);
      mBinding.setMyModel(new MyModel());
    }

    public void onClickAnItem(View view) {
      MyModel model = mBinding.getMyModel();
      model.setField1 = "Jhon";
      model.setField2 = "Dho";
      mBinding.executePendingBindings();
    }
}

在这种情况下,模型“MyModel”已被修改,但视图未更新;我错过了什么?

1 个答案:

答案 0 :(得分:2)

阅读文档我找到了一个解决方案,首先: 任何普通的旧Java对象(PO​​JO)都可以用于数据绑定,但修改POJO不会导致UI更新! 为了让MyModel数据对象能够在数据发生变化时通知我进行了这些修改:

SELECT CONCAT(fromTime, '-', toTime) AS halfHours, COUNT(created_at)
FROM 

(SELECT @time fromTime, ADDTIME(@time, '00:29:00')  toTime,  @time := ADDTIME(@time, '00:30:00') 
FROM information_schema.COLLATIONS
JOIN (SELECT @time := TIME('00:00:00')) a
WHERE @time < '24:00:00'
) timeTable

LEFT JOIN urls ON HOUR(created_at) BETWEEN HOUR(fromTime) AND HOUR(toTime) 
            AND MINUTE(created_at) BETWEEN MINUTE(fromTime) AND MINUTE(toTime) 
GROUP BY fromTime           

我希望这可以帮助别人 Documentation here