java - 双向数据绑定(bind) : Unable to change EditText value when value updated from an inner class

标签 java android data-binding

我正在尝试通过调用 profile.setClientName("Name"); 来更新我的 EditText 中的值来自 Observer<T>onChanged事件,但 EditText 不反射(reflect)更改。 如果从 onCreateView 调用上面的代码行,EditText 会更新。我的 fragment 。

这是我的代码:

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

客户端配置文件.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

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

<?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>

最佳答案

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

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

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

关于java - 双向数据绑定(bind) : Unable to change EditText value when value updated from an inner class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55830018/

相关文章:

java - 无法轻松注入(inject) CDI Bean

c++ - 使用 C++ 对象 Q_PROPERTY 绑定(bind)复选框 'checked' 属性

java - 如何在groovy中检查列表中的元素

java - 为 Java irc bot 设置客户端版本

关于尾随字符的 Android 计费异常

android - Listview 将新项目添加到适配器并进行设置

android - Android, flavor , flavor , flavor 和 flavor

c# - 绑定(bind)未按预期更新

WPF 数据绑定(bind) : Don't overwrite pre-populated values

java cassandra对象映射注释