亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

雙向數據綁定:從內部類更新值時無法更改 EditText 值

雙向數據綁定:從內部類更新值時無法更改 EditText 值

皈依舞 2022-10-20 17:29:53
我試圖通過profile.setClientName("Name");從Observer<T>'onChanged事件調用來更新我的 EditText 中的值,但 EditText 不反映更改。onCreateView如果從我的片段中調用上述代碼行,則 EditText 會更新。這是我的代碼:ClientProfileFragment.javapublic 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);                }            }        }    }}
查看完整描述

1 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

事實證明,我必須在為 egbinding.setClientprofile(profile);賦值后調用profile


profile = clientProfileWrapper.getData().get(0);

binding.setClientprofile(profile);

notifyPropertyChanged(BR._all);

這樣做會使用當前所需的值填充 EditText 字段。


查看完整回答
反對 回復 2022-10-20
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號