我试图通过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 字段。
添加回答
举报
0/150
提交
取消