Android:知道数据库中的所有项目何时将其字段设置为相同的值?

发布于 2024-10-14 10:31:16 字数 252 浏览 2 评论 0原文

我有一个应用程序,可以让用户选择列表中的每个项目并将其设置为他们的“最爱”。我认为,如果当所有项目都设置为最喜欢的项目时,按钮切换到“取消选择全部”,那就太好了。

我考虑这样做的方法是注册一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东京女 2024-10-21 10:31:16

这是一个解决方案,如果发现差异,应该(我相信)“快速中止”。

它避免了像选择不同或分组依据那样的排序/不同阶段。在某些情况下,这种方法可能会很慢,并且/或在其他情况下,这种方法的速度可能会非常快,可以忽略不计。 SQLite 非常快!另请记住,索引在 RDMBS 性能中发挥着良好的作用

内部选择可以用文字值替换,这会降低复杂性。无论如何,为了娱乐。

select exists
  (select * from t
   where val != (select val from t limit 1))

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.

select exists
  (select * from t
   where val != (select val from t limit 1))
捎一片雪花 2024-10-21 10:31:16

您也可以这样做

SELECT FieldName FROM Table GROUP BY FieldName

如果您的结果集超过 1 行,它们不相同

You could also do

SELECT FieldName FROM Table GROUP BY FieldName

If your result set has more than 1 row, they're not identical

你怎么这么可爱啊 2024-10-21 10:31:16

Basiclife 答案的变体:SELECT DISTINCT FieldName FROM Table

A variant to the answer by Basiclife: SELECT DISTINCT FieldName FROM Table

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文