把光标传给一个活动?[英] Passing a cursor to an activity?

本文是小编为大家收集整理的关于把光标传给一个活动?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

推荐答案

我个人不知道有任何简单的方法.在目标活动中再次进行查询可能会更容易.

其他推荐答案

另一种做到这一点的方法,可能更容易的是为您的应用创建应用程序类.保证仅创建一次,并且在您的应用程序的一生中存在.除其他外,它可以为您的应用程序提供"数据集线器"功能,因此不同的活动可以轻松共享数据.因此,对于您的光标,您只需使用应用程序类的成员变量(警告,我从应用程序复制了此代码并在此处进行了编辑,因此不保证编译.只是可以显示该想法的东西.):):

    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;
        }

    }

可以使用

从任何活动中获取应用程序对象
    this.getApplication()

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

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

因此,您的第一个活动将从数据库中获取一些傻瓜,该数据库作为光标返回到它.此活动将在应用程序中调用SetSharedCursor.然后,它将启动第二个活动,该活动将在其ongreate函数(或任何其他功能)中调用getSharedCursor来检索光标.

其他推荐答案

您应该编写自己的光标,该光标将实现可穿戴的接口.在这种情况下,您可以将光标放在包裹上,并通过putextra()将其发送到另一个活动.在目标活动中,您可以通过包裹方法之一(与粘合剂有关)爆炸(实际上只是通过处理程序找到它)光标.

本文地址:https://www.itbaoku.cn/post/102476.html

问题描述

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

推荐答案

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.

其他推荐答案

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.

其他推荐答案

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).