简体   繁体   English

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

[英]Two-way data binding: Unable to change EditText value when value updated from an inner class

I am trying to update the value in my EditText by calling profile.setClientName("Name"); 我正在尝试通过调用profile.setClientName("Name");来更新EditText中的值profile.setClientName("Name"); from an Observer<T> 's onChanged event, but the EditText doesn't reflect the changes. Observer<T>onChanged事件中获取,但是EditText不能反映更改。 The EditText updates if the above line of code is called from the onCreateView of my fragment. 如果从片段的onCreateView调用了上面的代码,则EditText将更新。

Here's my code: 这是我的代码:

ClientProfileFragment.java 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);
                }
            }
        }
    }
}

ClientProfile.java ClientProfile.java

public class ClientProfile extends BaseObservable {
    private String clientName;

    public ClientProfile() {
    }

    @Bindable
    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
        notifyPropertyChanged(BR.clientName);
    }
}

CPViewModel.java CPViewModel.java

public class CPViewModel extends ViewModel {
    private MutableLiveData<ClientProfile> clientProfile;

    public MutableLiveData<ClientProfile> getClientProfile() {
        if (clientProfile == null) {
            clientProfile = new MutableLiveData<>();
        }
        return clientProfile;
    }
}

client_profile_fragment.xml client_profile_fragment.xml

<?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"
    android:id="@+id/layout2">

    <data>

        <variable
            name="clientprofile"
            type="com.package.ClientProfile" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"/>
                <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/name_layout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/name_label">

                        <com.google.android.material.textfield.TextInputEditText
                            android:id="@+id/name_input"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:inputType="textPersonName"
                            android:text="@={clientprofile.clientName}"/>
                </com.google.android.material.textfield.TextInputLayout>
    </LinearLayout>
</layout>

It turns out that I had to call binding.setClientprofile(profile); 原来,我不得不调用binding.setClientprofile(profile); after assigning value to profile eg 将值分配给profile例如

profile = clientProfileWrapper.getData().get(0);
binding.setClientprofile(profile);
notifyPropertyChanged(BR._all);

Doing this populated the EditText field with the current required value. 这样做会用当前所需的值填充EditText字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM