Android:知道数据库中的所有项目何时将其字段设置为相同的值?
我有一个应用程序,可以让用户选择列表中的每个项目并将其设置为他们的“最爱”。我认为,如果当所有项目都设置为最喜欢的项目时,按钮切换到“取消选择全部”,那就太好了。
我考虑这样做的方法是注册一个 ContentObserver,每当进行更改时都会检查所有项目现在是否都设置为收藏夹。然后我意识到这可能不是很有效率。但我想不出任何其他方法,所以有什么提示吗?
我如何知道数据库中的所有项目都将其字段之一设置为相同的值(在本例中,字段 favorite = 1)?
I have an app that lets the user select every item in the list and set it as their "favorite". I thought it would be neat if, when all the items are set as favorite, the button switched to "unselect all".
The way I'm thinking about doing this is by registering a ContentObserver, which whenever a change was made checked if all the items are now set as favorite. Then I realized that may not be very efficient. I can't think of any other way, though, so any hints?
How can I know that all the items in the database have one of their fields set to the same value (field favorite = 1, in this case)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个解决方案,如果发现差异,应该(我相信)“快速中止”。
它避免了像选择不同或分组依据那样的排序/不同阶段。在某些情况下,这种方法可能会很慢,并且/或在其他情况下,这种方法的速度可能会非常快,可以忽略不计。 SQLite 非常快!另请记住,索引在 RDMBS 性能中发挥着良好的作用。
内部选择可以用文字值替换,这会降低复杂性。无论如何,为了娱乐。
Here is a solution that should (I believe) "abort fast" if a difference is found.
It avoids the sorting/distinct phase as with select distinct or group by. This approach may very well be slower in some situations and/or only very negligibly faster in others. SQLite is very darn fast! Also remember that indexes play a good role in RDMBS performance.
The inner select could be replaced with a literal value which would reduce the complexity. In any case, for amusement.
您也可以这样做
如果您的结果集超过 1 行,它们不相同
You could also do
If your result set has more than 1 row, they're not identical
Basiclife 答案的变体:
SELECT DISTINCT FieldName FROM Table
A variant to the answer by Basiclife:
SELECT DISTINCT FieldName FROM Table