从 Android SQLite 游标中删除行
我查询并获取结果集,但我需要执行一些在 SQLite WHERE 子句中不可能进行的计算,以确定 ListView 中显示的内容。如何从游标中删除某些行?我知道这与这个 Filter 是同一个问题来自 Cursor 的行,因此它们不会显示在 ListView 中,但这个答案没有帮助。如果没有更简单的方法可以提供示例吗?
I query and get a result set back, but I need to do some calculations that are impossible in the SQLite WHERE clause in order to determine what shows up in the ListView. How can I remove certain rows from the cursor? I know it is the same question as this Filter rows from Cursor so they don't show up in ListView but that answer does not help. Can an example be provided if there isn't a simpler way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单地保留光标中的所有行,然后使用自定义适配器在显示时隐藏不需要的行可能会起作用。例如,如果您扩展
CursorAdapter
,那么您的bindView
实现中可能会有类似这样的内容:It might work to simply retain all the rows in the Cursor, but then use a custom adapter to hide the unwanted rows at display time. For example, if you extend
CursorAdapter
, then you might have something like this in yourbindView
implementation:应该有更好的方法来做到这一点,但我最终做的是将我想要的每一行的 ID 存储在字符串 ArrayList 中,然后重新查询 where _id IN arraListOfIds.toString(),用括号替换方括号以适应SQL 语法。
There should be a better way to do this, but what I ended up doing is storing the ID of each row I wanted in a string ArrayList, and then requerying where _id IN arraListOfIds.toString(), replacing the square brackets with parentheses to fit SQL syntax.
上面的“What Columns”部分是您可以告诉光标要返回哪些行的地方。使用“null”会返回全部。
The "What Columns" piece above is where you can tell the cursor which rows to return. Using "null" returns them all.
这很难相信;我的经验是,SQL 可以让您查询几乎任何您需要的内容(SQLite 中的分层或递归查询除外)。如果您需要的某些功能不受支持,您可以使用 sqlite_create_function() 轻松添加它并在您的应用程序中使用它。或者,也许创造性地使用 SELECT 子句可以满足您的需求。
你能解释一下这些不可能的计算是什么吗?
编辑:没关系,查看 此网页 显示sqlite_create_function() 适配器全部由 Android SQLite 包装器封闭。这很烦人。
I find this very hard to believe; my experience has been that SQL will let you query for just about anything you'd ever need (with the exception of heirarchical or recursive queries in SQLite's case). If there's some function you need that isn't supported, you can add it easily with sqlite_create_function() and use it in your app. Or perhaps a creative use of the SELECT clause can do what you are looking for.
Can you explain what these impossible calculations are?
EDIT: Nevermind, checking out this webpage reveals that the sqlite_create_function() adapter is all closed up by the Android SQLite wrapper. That's annoying.