MutableRealmInteger

时间:2018-03-05 13:20:22

标签: android data-binding realm

我正在使用Realmdata bindingMutableRealmInteger之前,我使用常规基本类型并使用其setter,并且UI随新值自动更改 现在我想使用MutableRealmInteger类型,但我不知道如何通知UI更改。

这是我模特的一部分:

public class MyModel extends RealmObject implements Observable {
...
public final MutableRealmInteger NewWayAAA = MutableRealmInteger.valueOf(0);
private String oldWayAAA = "0";
...

@Bindable
public String getOldWayAAA() {
    return oldWayAAA.isEmpty() ? "0" : oldWayAAA;
}

public void setOldWayAAA (String param) {
    if (!param.isEmpty()) {
        this.oldWayAAA= param;
        notifyPropertyChanged(BR.oldwayaaa);
    }
}

...

/**
 * Notifies listeners that all properties of this instance have changed.
 */
public synchronized void notifyChange() {
    if (mCallbacks != null) {
        mCallbacks.notifyCallbacks(this, 0, null);
    }
}

/**
 * Notifies listeners that a specific property has changed. The getter for the property
 * that changes should be marked with {@link Bindable} to generate a field in
 * <code>BR</code> to be used as <code>fieldId</code>.
 *
 * @param fieldId The generated BR id for the Bindable field.
 */
public void notifyPropertyChanged(int fieldId) {
    if (mCallbacks != null) {
        mCallbacks.notifyCallbacks(this, fieldId, null);
    }
}

@Override
public void addOnPropertyChangedCallback(OnPropertyChangedCallback onPropertyChangedCallback) {
    if (mCallbacks == null) {
        mCallbacks = new PropertyChangeRegistry();
    }
    mCallbacks.add(onPropertyChangedCallback);
}

@Override
public void removeOnPropertyChangedCallback(OnPropertyChangedCallback onPropertyChangedCallback) {
    if (mCallbacks != null) {
        mCallbacks.remove(onPropertyChangedCallback);
    }
}

MutableRealmInteger没有设置者,其用法是:

myModelIns.NewWayAAA.increment(10);

1 个答案:

答案 0 :(得分:1)

public class MyModel extends RealmObject implements Observable {
    ...
    private final MutableRealmInteger NewWayAAA = MutableRealmInteger.valueOf(0);
    ...

    @Bindable
    public int getNewWayAAA() {
        return NewWayAAA.getValue();
    }

    public void incrementNewWayAAA() {
        NewWayAAA.increment();
        notifyPropertyChanged(BR.newwayaaa);
    }

    ...