android - Observable Notify 多个变量变化
问题描述
如何通知多个变量更新?
有什么方法可以检测和分离notifyObservers吗?
public class AnimalWeightObservable extends Observable {
public long animalId;
public long weigh;
public long getAnimalId() {
return animalId;
}
public AnimalWeightObservable setAnimalId(long animalId) {
this.animalId = animalId;
this.setChanged();
this.notifyObservers(animalId);
return this;
}
public long getWeigh() {
return weigh;
}
public AnimalWeightObservable setWeigh(long weigh) {
this.weigh = weigh;
this.setChanged();
this.notifyObservers(weigh);
return this;
}
}
如何检测女巫变量是否发生了变化?
解决方案
如何包装animalId
和weight
内部另一种类型:例如AnimalProperty
class AnimalProperty<T> {
String propertyName;
T property;
AnimalProperty(String name, T property) {
this.propertyName = name;
this.property = property;
}
}
所以你的代码看起来像这样:
public class AnimalWeightObservable extends Observable {
public AnimalProperty animalId;
public AnimalProperty weigh;
//...
//...
public AnimalWeightObservable setWeigh(AnimalProperty weigh) {
this.weigh = weigh;
this.setChanged();
this.notifyObservers(weigh);
return this;
}
}
然后在Observer
'supdate(...)
方法中打开 就propertyName
知道哪个属性被更新了
推荐阅读
- apache-camel - Camel + pollEnrich from bean
- r - 将 dplyr 子表检测为函数而不是表
- robotframework - RobotFrameWork:如何检查组件块顶行中的动态元素
- azure - 没有 /FileStore 和 /Users 的 Azure Databricks
- hibernate - Hibernate 在 logback 中显示没有时间戳格式的 sql 日志
- maven - 找不到 com.android.support:appcompat-v7:28.0.0
- css - 页脚和页眉重叠内容
- javascript - 可以根据fabricJs中的宽高来确定字体大小吗?
- php - WordPress - 为什么这个元查询在日期查询之外返回结果?
- javascript - Firestore 数据以错误的顺序获取