猿问

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

我试图通过profile.setClientName("Name");从Observer<T>'onChanged事件调用来更新我的 EditText 中的值,但 EditText 不反映更改。onCreateView如果从我的片段中调用上述代码行,则 EditText 会更新。


这是我的代码:


ClientProfileFragment.java


public 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);

                }

            }

        }

    }

}


皈依舞
浏览 122回答 1
1回答

守着一只汪

事实证明,我必须在为 egbinding.setClientprofile(profile);赋值后调用profileprofile = clientProfileWrapper.getData().get(0);binding.setClientprofile(profile);notifyPropertyChanged(BR._all);这样做会使用当前所需的值填充 EditText 字段。
随时随地看视频慕课网APP

相关分类

Java
我要回答