【问题标题】:Two-way data binding: Unable to change EditText value when value updated from an inner class双向数据绑定:从内部类更新值时无法更改 EditText 值
【发布时间】:2019-04-24 12:25:11
【问题描述】:

我试图通过从Observer<T>onChanged 事件调用profile.setClientName("Name"); 来更新我的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);
                }
            }
        }
    }
}

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

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>

【问题讨论】:

  • 分享你的viewmodel类
  • 触发观察者onChanged的事件是什么?
  • @MehulKabaria 已更新。
  • @Onheiron 我正在在线获取 ClientProfile 数据。一旦 json 被反序列化,我就会调用mViewModel.getClientProfile().setValue(deserializedClientProfile);
  • 你的mViewModel 班级怎么样?似乎您执行了太多的设置操作,要么设置新的 ClientProfile 值,要么设置新的名称值。

标签: java android data-binding


【解决方案1】:

原来我在给profile赋值后不得不调用binding.setClientprofile(profile); 例如

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 2020-09-10
    相关资源
    最近更新 更多