如何将数据库适配器传递给另一个活动?

发布于 2024-11-06 03:26:26 字数 564 浏览 0 评论 0原文

我在理解 Android SDK 中的搜索对话框时遇到一些困难。

我的应用程序的“主要活动”提供了一个按钮。如果用户单击此按钮,则会调用搜索对话框。然后搜索本身在异步任务中完成,因为它可能需要一些时间。到目前为止,一切都很好。

主活动还创建一个数据库适配器对象,用于初始化数据库、执行查询等。但是如何在可搜索活动中使用此适配器对象呢?

主要活动
// 初始化数据库
DatabaseAdapter dba = new DatabaseAdapter();
dba.init();
// 打开搜索对话框
if (buttonClick) onSearchRequest();

可搜索活动

  1. 获取意图并从搜索对话框接收查询 -> OK
  2. 如何再次使用数据库适配器来执行查询?

我必须创建一个新对象吗?我可以以某种方式将其从最小活动传递到可搜索活动吗,[...]?

谢谢,
罗伯特

I'm having some trouble understanding the search dialog in the Android SDK.

The "main activity" of my application provides a button. If the user clicks this button the search dialog is called. The search itself is then done in an async task as it might need some time. So far so good.

The main activity also creates a database adapter object which is used to initialize the database, perform queries, etc. But how can I use this adapter object in the searchable activity?

MAIN activity
// Init database
DatabaseAdapter dba = new DatabaseAdapter();
dba.init();
// open search dialog
if (buttonClick) onSearchRequest();

Searchable activity

  1. Get intent and receive query from search dialog -> OK
  2. How can I use the database adapter again to perform a query?

Do I have to create a new object? Can I pass it somehow from the min activity to the searchable activity, [...]?

Thanks,
Robert

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

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

发布评论

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

评论(3

纸短情长 2024-11-13 03:26:26

您可以在应用程序类中创建适配器,并在所有活动中检索它。这就是我为我的项目所做的。

public class ApplicationClass extends Application {

    Adapter adapter.

    @Override 
    public void onCreate(){
    adapter=new Adapter();
        super.onCreate();
    }

    public Adapter getAdapter(){
        return adapter;
    }

}

然后从 Activity 调用:

Adapter adapter=(ApplicationClass)getApplication().getAdapter();

类似的事情。 ApplicationClass 是您的应用程序名称。可能是 MyAppNameApplication 您应该在包中创建它,然后在 AndroidManifest.xml 中声明

You can create adapter in Application class, and retreieve it in all your activities. That's what I do for my projects.

public class ApplicationClass extends Application {

    Adapter adapter.

    @Override 
    public void onCreate(){
    adapter=new Adapter();
        super.onCreate();
    }

    public Adapter getAdapter(){
        return adapter;
    }

}

Then call from Activity:

Adapter adapter=(ApplicationClass)getApplication().getAdapter();

Something like that. ApplicationClass is for your app name. Could be MyAppNameApplication You should create it in your package and then declare in AndroidManifest.xml

梦明 2024-11-13 03:26:26

一种选择是使用单例并通过静态方法提供对 DatabaseAdapter 的访问。例如:

private static DatabaseAdapter sWritableAdapter = null;
private static DatabaseAdapter sReadableAdapter = null;

public static DatabaseAdapter openForReading(Context ctx) {
    if(sReadableAdapter == null)
    sReadableAdapter = new DatabaseAdapter(new DatabaseHelper(ctx).getReadableDatabase());

    return sReadableAdapter;

}

或用于写入访问:

public static DatabaseAdapter openForWriting(Context ctx) {
if(sWritableAdapter == null)
        sWritableAdapter = new DatabaseAdapter(new DatabaseHelper(ctx).getWritableDatabase());

    return sWritableAdapter;

}

因此,在您的可搜索活动中,您可以编写例如:

DatabaseAdapter adapter = DatabaseAdapter.openForReading(ctx);
adapter.searchSomething();

Marco

An option would be to use a singleton and provide access to the DatabaseAdapter via a static method. Ex:

private static DatabaseAdapter sWritableAdapter = null;
private static DatabaseAdapter sReadableAdapter = null;

public static DatabaseAdapter openForReading(Context ctx) {
    if(sReadableAdapter == null)
    sReadableAdapter = new DatabaseAdapter(new DatabaseHelper(ctx).getReadableDatabase());

    return sReadableAdapter;

}

or for write access:

public static DatabaseAdapter openForWriting(Context ctx) {
if(sWritableAdapter == null)
        sWritableAdapter = new DatabaseAdapter(new DatabaseHelper(ctx).getWritableDatabase());

    return sWritableAdapter;

}

So in your searchable activity you would write for instance:

DatabaseAdapter adapter = DatabaseAdapter.openForReading(ctx);
adapter.searchSomething();

Marco

浪漫人生路 2024-11-13 03:26:26

您应该实现 ContentProvider

http://developer.android.com/guide /topics/providers/content-providers.html

它们是单例,并且可以从(几乎)应用程序中的任何位置访问。

You should rather implement an ContentProvider

http://developer.android.com/guide/topics/providers/content-providers.html

They are singletons, and accessible from (almost) everywhere in your application.

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