While implementing a solution based on Realm Mobile Database, RecyclerView and DataBinding I am stuck in receiving a change notification from an edittext view (within recyclerview) that is bound to a realm model. I tried it with RxBinding (my favourite) but without success - when it came to item.setName()... It crashed.
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
I changed the code to:
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
Now it's working, but: - is this the way it should be implemented? I will add primary keys but I want to prevent the items from being queried when the recyclerview is loaded. Is that possible? And the most important question: how do I unsubscribe?