在实施基于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时查询项目。那可能吗?最重要的问题是:如何取消订阅?