非活性类Access数据库 [英] Access database in non-Activity class

查看:102
本文介绍了非活性类Access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类( SomeClass.class )。我希望有一些静态的方法,它像 getAllDatabaseItems ,getTableItems,insertNewRecord等。

I have some class (SomeClass.class). I want to have some static methods in it like getAllDatabaseItems, getTableItems, insertNewRecord and so on.

如果我这样做,这样

SQLiteDatabase db = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);

  • 我需要延长活动(但仍不能在静态方法使用它),或通过DB变量中的每一个方法(从来电者的活动),这是pretty的笨重。
  • 有什么解决办法,所以我可以从一些一流的呼叫 SomeClass.getAllDatabaseItems()

    What's the solution so I can from some class call SomeClass.getAllDatabaseItems()?

    @ MobileDev123 所以我还需要延长的活动(因为该方法的openOrCreateDatabase )? 如果我有这个类(它实际上不是一个活动,我不使用这种方式)

    @MobileDev123 So I still need to extend Activity (because of the method openOrCreateDatabase)? If I have this class (which isn't actually an activity, I don't use it that way)

    public class Partner extends Activity {
    @SuppressWarnings("static-access")
    public Partner(Context mContext) {
        myContext = mContext;
        db = openOrCreateDatabase(DATABASE_NAME, myContext.MODE_PRIVATE, null);
    
        db.execSQL("CREATE TABLE IF NOT EXISTS " + PARTNER_TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR);");
        db.execSQL("CREATE TABLE IF NOT EXISTS " + ADDRESS_TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " + PARTNER_ID + " INT, " + ADDRESS + " VARCHAR, " + CITY + " VARCHAR);");
    }
    

    再从我的一些activites这样称呼它

    And then call it from some of my activites like this

        Partner newPartner = new Partner(this);
        partnersItems = newPartner.getAllItems();
    

    我得到的第4行(Partner.class)的NullExceptionError - 为什么?如果我使用静态引用的

    I get an NullExceptionError on line 4 (Partner.class) - why? If I use static reference on

    MODE_PRIVATE => (Context.MODE_PRIVATE)
    

    再次它不工作。

    again it's not working.

    @Falmarri 同静态的,如果我通过在本的说法(一些主叫类),并接受它作为我的静态方法的上下文参数仍无法成功创建/打开我的数据库(参见前行)

    @Falmarri same with static, if I pass in "this" argument (from some caller class) and receive it as an Context argument in my static method still can't successfully create/open my database (see lines before)

    推荐答案

    必须有它调用类的活动或服务,你所能做的就是通的您方便的时间。 (我preFER通过它的构造函数)。

    There must be an activity or service which calls your class, what you can do is pass this at your convenient time. (I prefer to pass it in constructor).

    在收到手使用的情况下瞬间。

    On receiving hand use the context instant.

    例如,从 MyActivity 类,你可以调用的CreateDatabase(本​​)新的DataService的(这一点) DataService的类的参数类型必须是上下文而不是 MyActivity

    E.g From MyActivity class you can call createDatabase(this) or new DataServices(this) but in DataServices class the argument type must be context instead of MyActivity.

    现在你有上下文参数,你可以在你想要包括调用的方式使用它 openOrCreateDatabase()

    Now you have the context parameter and you can use it in the way you want including calling openOrCreateDatabase() .

    修改:添加code

    从Main.java

    from Main.java

    DataBase database = new DataBase(this); //This will pass an instance of main. Which is eventually the subclass of Context.java
    

    数据库类:你并不需要延长在那里的活动。在构造函数中认定中的

    In DataBase class: you don't need to extend activity there. In constructor defination

    公共数据库(上下文的背景下); //如果您使用的是Eclipse和依靠一些自动化的工具,你可以看到这样的主力主攻。但是,使用这些行,所以你可以从任何活动或服务通过这个称呼它。

    public DataBase(Context context); //If you are using eclipse and rely on some automated tools you can see something like Main main. But use these line, so you can call it from any activity or service by passing this.

    定义上下文类的领域,它指的是环境ARG。

    define a field of Context class, and refer it to the context arg.

    如同 this.localContext =背景;

    和使用localContext变量,你可以调用openOrCreateDataBase列。

    And by using localContext variable you can call openOrCreateDataBase column.

    加成:如果您有连接到这一点,你可以实例数据库的任何控制(视子类),通过调用新的数据库(view.getContext());

    ADDITION: If you have any control (subclass of view) attached to this you can instantiate DataBase by calling new DataBase(view.getContext());

    我希望这会帮助你....如果需要更多的帮助,请随时下面发表评论。

    I hope this will help you.... in case any more help needed feel free to comment below.

    这篇关于非活性类Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆