创建一个值观察器并从任何 activity 更新 textview/list 视图
Create a value observer and update textview/list view from any activity
设置
我在 activity 中有一个文本视图。这本质上是一个通知计数视图。单击此按钮会打开一个对话框,其中包含包含通知的列表视图。现在,无论我在 activity 哪个位置,我都可以收到通知。 textview 存在于主 activity 的主片段中。应用中有很多这样的片段和各种活动。
数据来自哪里
有一项服务从 Web 服务下载一些数据并将其放入全局数组列表中 Constants.arraylist
。通知计数存储在另一个全局变量中,比如 Constants.notifcount
。此服务可以随时 运行。现在我需要用更新的数据更新上面设置中的 textview 和 listview,无论我在哪个 activity 当这个后台服务完成 运行ning 时。由于各种业务逻辑原因,我无法将服务绑定到主 activity
所以 textview 应该更新到最新的 Constants.notifcount
并且 listview 应该更新到最新的 Constants.arraylist
注意 --> 我知道全局变量的设计很糟糕,我会在完成此 POC 后找出更好的方法。
问题
Is it possible
1.) To Create a listener on the variables such that no matter where I am these two data fields can be updated immediately after the service
runs.
2.) Is there a better way to do this ? Or better yet what is the industry best practice for creating a run time notifier like this? (Dataset observables? Loopers?)
无论你的文本视图在哪里,注册一个广播接收器。当通知计数发生变化时,通过接收器广播它并更新您的文本视图。您甚至可以将 textview 子类化,添加一个 运行 时间广播接收器(即,在 onAttachedToWindow 上注册它并在 DetachedFromWindow 上注销它)。那么无论 textview 在哪里,只要服务 运行ning 就可以通知 textview。
设置
我在 activity 中有一个文本视图。这本质上是一个通知计数视图。单击此按钮会打开一个对话框,其中包含包含通知的列表视图。现在,无论我在 activity 哪个位置,我都可以收到通知。 textview 存在于主 activity 的主片段中。应用中有很多这样的片段和各种活动。
数据来自哪里
有一项服务从 Web 服务下载一些数据并将其放入全局数组列表中 Constants.arraylist
。通知计数存储在另一个全局变量中,比如 Constants.notifcount
。此服务可以随时 运行。现在我需要用更新的数据更新上面设置中的 textview 和 listview,无论我在哪个 activity 当这个后台服务完成 运行ning 时。由于各种业务逻辑原因,我无法将服务绑定到主 activity
所以 textview 应该更新到最新的 Constants.notifcount
并且 listview 应该更新到最新的 Constants.arraylist
注意 --> 我知道全局变量的设计很糟糕,我会在完成此 POC 后找出更好的方法。
问题
Is it possible
1.) To Create a listener on the variables such that no matter where I am these two data fields can be updated immediately after the service runs.
2.) Is there a better way to do this ? Or better yet what is the industry best practice for creating a run time notifier like this? (Dataset observables? Loopers?)
无论你的文本视图在哪里,注册一个广播接收器。当通知计数发生变化时,通过接收器广播它并更新您的文本视图。您甚至可以将 textview 子类化,添加一个 运行 时间广播接收器(即,在 onAttachedToWindow 上注册它并在 DetachedFromWindow 上注销它)。那么无论 textview 在哪里,只要服务 运行ning 就可以通知 textview。