Я пытаюсь обновить значение в моем EditText, вызывая profile.setClientName("Name");
из события Observer<T>
s onChanged
, но EditText не отражает изменения.EditText обновляется, если указанная выше строка кода вызывается из onCreateView
моего фрагмента.
Вот мой код:
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>