如何将额外的元素插入 SimpleCursorAdapter 或 Spinner 的 Cursor 中?
我有一个 Spinner,用于显示从数据库获取的数据列表。数据从查询返回到游标,然后游标被传递到 spinner 的 SimpleCursorAdapter。它工作正常,但我想在此数据之上插入另一个项目。例如,微调器已经显示了保存在数据库中的用户创建的模板列表,但我想在模板列表顶部插入“新模板”和“空模板”,并且需要将其插入到 Cursor/SimpleCursorAdapter 中不知何故。
我考虑过使用数组列表并从游标填充数组列表,但游标对我来说是更好的解决方案,因为它也包含其他相关的数据行。我在互联网上搜索其他解决方案,并找到了一些要求使用 CursorWrapper 来实现此目的的答案,但我找不到如何使用 CursorWrapper 来完成我想要的任务的具体示例。我如何在游标中插入一些行,或者有人可以给出一个易于理解的 CursorWrapper 示例!!提前致谢。
I have a Spinner which is to show a list of data fetched from database. The data is returned to a cursor from query, and the cursor gets passed to spinner's SimpleCursorAdapter. It is working fine as such, but I want to insert another item on top of this data. For example, the spinner is already showing a list of user created templates saved in DB, but I want to insert "New Template" and "Empty Template" on top of the list of templates, and it needs to be inserted into Cursor/SimpleCursorAdapter somehow.
I have considered using an arraylist and populating the arraylist from cursor, but cursor is better solution for me since it contains other related rows of data too. I searched internet for other solutions and found some answers asking to use CursorWrapper for this purpose, but I could not find a concrete example how to use CursorWrapper to accomplish what I want. How can I insert some rows in cursor or can someone please give a easy to follow CursorWrapper example!! Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将
MergeCursor
和MatrixCursor
与数据库cursor
结合使用,如下所示:You can use a combination of
MergeCursor
andMatrixCursor
with your DBcursor
like this:这是我尝试过的方法。
DBHelper.insertRow()
使用此方法,您可以在游标中的任何位置添加任意数量的行。即使它没有使用 CursorWrapper,它也可以与 CursorAdapters 或 SimpleCursorAdapters 一起使用。
This is the method I tried.
DBHelper.insertRow()
With this method, you can add any amount of rows anywhere in your cursor. Even though it is not making use of CursorWrapper, it can be used with CursorAdapters or SimpleCursorAdapters.
我尝试了@naktinis提供的解决方案,但结果不是我所期望的。我自己想要实现的目标是作为一个适配器,其中可以在顶部添加新元素(索引 0)。然而,根据给出的解决方案,新元素确实被添加到顶部,但仅添加到 MatrixCursor 的末尾。换句话说,当我动态地将行添加到“extras”MatrixCursor时,我得到了这样的结果:
但是,我真正想要实现的是这样的:
换句话说,最近的元素在顶部输入(索引0)。
我可以通过执行以下操作手动实现此目的。请注意,我没有包含任何处理从适配器动态删除元素的逻辑。
I tried the solution provided by @naktinis, but the result wasn't what I expected. What I myself wanted to achieve as an adapter in which new elements can be added at the top (index 0). However, with the solution given, new elements were indeed added at the top but only to the END of the MatrixCursor. In other words, when I added rows dynamically to the "extras" MatrixCursor, I got something like this:
However, what I really wanted to achieve was something like this:
In other words, most recent elements enter at the top (index 0).
I was able to achieve this manually by doing the follow. Note that I did not include any logic to handle dynamically removing elements from the adapter.