将光标传递给活动?

发布于 2024-09-27 14:25:20 字数 51 浏览 4 评论 0原文

这可能吗?我正在尝试在一个活动中打开 SQLite 数据库游标,并将其传递给另一个活动。

Is this possible? I am trying to open a SQLite database cursor in one activity, and pass it to another activity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

北方的韩爷 2024-10-04 14:25:20

另一种可能更简单的方法是为您的应用程序创建一个应用程序类。这保证只创建一次,并且在您的应用程序的生命周期中存在。除此之外,它还可以为您的应用程序提供“数据中心”功能,以便不同的 Activity 可以轻松共享数据。因此,对于您的光标,您只需使用 Application 类的成员变量,如下所示(警告,我从我的应用程序复制了此代码并在此处进行编辑,因此不能保证编译。只是展示一下想法。):

    package com.jcascio.k03;

    import android.app.Application;
    import android.database.Cursor;

// use your application's name instead of "K03Application"

        public class K03Application extends Application { 

        public final String TAG = "K03";

        Cursor sharedCursor; // this cursor can be shared between different Activities

        @Override
        public void onCreate() {
            super.onCreate();
        }

        @Override
        public void onTerminate() {
            super.onTerminate();
        }


        public Cursor getSharedCursor() 
        {
            return this.sharedCursor;
        }

        public void setSharedCursor(Cursor c)
        {
            this.sharedCursor = c;
        }

    }

可以使用以下方法从任何 Activity 中获取应用程序对象。

    this.getApplication()

// You cast it to your Application sub-class and call the Cursor accessor function

Cursor c = ((K03Application)this.getApplication()).getSharedCursor();

因此,您的第一个 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.):

    package com.jcascio.k03;

    import android.app.Application;
    import android.database.Cursor;

// use your application's name instead of "K03Application"

        public class K03Application extends Application { 

        public final String TAG = "K03";

        Cursor sharedCursor; // this cursor can be shared between different Activities

        @Override
        public void onCreate() {
            super.onCreate();
        }

        @Override
        public void onTerminate() {
            super.onTerminate();
        }


        public Cursor getSharedCursor() 
        {
            return this.sharedCursor;
        }

        public void setSharedCursor(Cursor c)
        {
            this.sharedCursor = c;
        }

    }

The application object can be fetched from any Activity using

    this.getApplication()

// You cast it to your Application sub-class and call the Cursor accessor function

Cursor c = ((K03Application)this.getApplication()).getSharedCursor();

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.

挽袖吟 2024-10-04 14:25:20

我个人不知道有什么简单的方法可以做到这一点。在目标活动中再次进行查询可能会更容易。

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.

記柔刀 2024-10-04 14:25:20

您应该编写自己的 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).

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