1

在实现基于 Realm Mobile Database、RecyclerView 和 DataBinding 的解决方案时,我卡在从绑定到领域模型的 edittext 视图(在 recyclerview 中)接收更改通知。我用 RxBinding (我最喜欢的)尝试了它,但没有成功 - 当它涉及 item.setName() 时......它崩溃了。

final int row = vh.getAdapterPosition();
    final android.widget.EditText editText = (EditText)vh.itemView.findViewById(R.id.firstLine);
    Subscription editTextSub =
            RxTextView.textChanges(editText)
                    .subscribe(new Action1<CharSequence>() {
                        @Override
                        public void call(CharSequence charSequence) {
                            if (row != RecyclerView.NO_POSITION) {
                                CrngyMaster item = (CrngyMaster) getItem(row);
                                item.setName(charSequence.toString());
                            }
                        }
                    });
    // Make sure to unsubscribe the subscription

我将代码更改为:

Subscription editTextSub =
            RxTextView.textChanges(editText)
                    .subscribe(new Action1<CharSequence>() {
                        @Override
                        public void call(CharSequence charSequence) {
                            int row = vh.getAdapterPosition();
                            if (row != RecyclerView.NO_POSITION) {
                                CrngyMaster item = (CrngyMaster) getItem(row);
                                String oldName = item.getName();
                                String modified = charSequence.toString();
                                if(Objects.equals(oldName, modified) == false) {
                                    CrngyMaster original = realm.where(CrngyMaster.class)
                                            .equalTo("mName", oldName)
                                            .findFirst();

                                    realm.beginTransaction();
                                    original.setName(modified);
                                    realm.commitTransaction();
                                }
                            }
                        }
                    });
    // Make sure to unsubscribe the subscription

现在它正在工作,但是: - 这是应该实施的方式吗?我将添加主键,但我想防止在加载 recyclerview 时查询项目。那可能吗?还有最重要的问题:如何退订?

4

0 回答 0