将光标传递给活动?
这可能吗?我正在尝试在一个活动中打开 SQLite 数据库游标,并将其传递给另一个活动。
Is this possible? I am trying to open a SQLite database cursor in one activity, and pass it to another activity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一种可能更简单的方法是为您的应用程序创建一个应用程序类。这保证只创建一次,并且在您的应用程序的生命周期中存在。除此之外,它还可以为您的应用程序提供“数据中心”功能,以便不同的 Activity 可以轻松共享数据。因此,对于您的光标,您只需使用 Application 类的成员变量,如下所示(警告,我从我的应用程序复制了此代码并在此处进行编辑,因此不能保证编译。只是展示一下想法。):
可以使用以下方法从任何 Activity 中获取应用程序对象。
因此,您的第一个 Activity 将从数据库中获取一些粘性物质,这些粘性物质作为游标返回给它。此活动将在应用程序中调用 setSharedCursor。然后它将启动第二个 Activity,该 Activity 将在其 onCreate 函数(或任何其他函数)中调用 getSharedCursor 来检索光标。
Another way to do this which might be easier is to create an Application class for your app. This is guaranteed to be created only once, and exists for the lifetime of your app. Among other things, it can provide a "data hub" capability for your app so different Activities can share data easily. So, for your cursor, you'd simply use a member variable of the Application class like so (warning, I copied this code from my app and edited it here, so no guarantee of compilation. Just something to show the idea.):
The application object can be fetched from any Activity using
So, your first Activity would fetch some goo from the database, which is returned to it as a Cursor. This activity would call setSharedCursor in the application. Then it would launch the second Activity, which would call getSharedCursor in its onCreate function (or any other function for that matter) to retrieve the cursor.
我个人不知道有什么简单的方法可以做到这一点。在目标活动中再次进行查询可能会更容易。
I personally don't know of any simple ways to do this. It might be easier just to make the query again in the destination activity.
您应该编写自己的 Cursor 来实现 Parcelable 接口。在这种情况下,您可以将光标放在 Parcel 上,然后通过 putExtra() 将其发送到另一个 Activity。在目标Activity中你可以通过Parcel方法之一(与Binder相关)来爆炸(实际上只是通过handler找到它)Cursor。
You should write your own Cursor which will implement Parcelable interface. In this case you can put your cursor to parcel and send it to another Activity through putExtra(). In target Activity you can explode (in fact just find it through handler) Cursor through one of Parcel methods (related to Binder).