为了账号安全,请及时绑定邮箱和手机立即绑定

双向数据绑定:从内部类更新值时无法更改 EditText 值

双向数据绑定:从内部类更新值时无法更改 EditText 值

皈依舞 2022-10-20 17:29:53
我试图通过profile.setClientName("Name");从Observer<T>'onChanged事件调用来更新我的 EditText 中的值,但 EditText 不反映更改。onCreateView如果从我的片段中调用上述代码行,则 EditText 会更新。这是我的代码:ClientProfileFragment.javapublic class ClientProfileFragment extends Fragment implements View.OnClickListener {    private ClientProfile profile; //The BaseObservable     private CPViewModel mViewModel;    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,                         @Nullable Bundle savedInstanceState) {        ...        ClientProfileFragmentBinding binding = DataBindingUtil.inflate(inflater,            R.layout.client_profile_fragment, container, false);        clientProfileView = binding.getRoot();        profile = new ClientProfile();        binding.setClientprofile(profile);        final Observer<ClientProfile> clientProfileObserver = new Observer<ClientProfile>() {            @Override            public void onChanged(ClientProfile clientProfile) {            profile.setClientName("Name"); //This line gets executed. Confirmed.            }        };        mViewModel.getClientProfile().observe(this, clientProfileObserver);        //If I call profile.setClientName("Name"); from here then the corresponding        //EditText changes to "Name".        return clientProfileView;    }    @Override    public void onClick(View v) {        customerFindFuture.then(new FutureCallback<Response<String>>() {            @Override            public void onCompleted(Exception e, Response<String> result) {                Gson gson = new GsonBuilder().serializeNulls().create();                ClientProfileWrapper clientProfileWrapper =                            gson.fromJson(result.getResult(), ClientProfileWrapper.class);                profile = clientProfileWrapper.getData().get(0);                mViewModel.getClientProfile().setValue(profile);                }            }        }    }}
查看完整描述

1 回答

?
守着一只汪

TA贡献1872条经验 获得超4个赞

事实证明,我必须在为 egbinding.setClientprofile(profile);赋值后调用profile


profile = clientProfileWrapper.getData().get(0);

binding.setClientprofile(profile);

notifyPropertyChanged(BR._all);

这样做会使用当前所需的值填充 EditText 字段。


查看完整回答
反对 回复 2022-10-20
  • 1 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号