Ormlite设置,而无需使用基地活动基地、Ormlite

2023-09-05 07:56:07 作者:烂人 !

我使用 ORMLite 在Android项目,我不想使用扩展活动,因为我将值插入在一个AsyncTask的数据库中。

I'm using ORMLite in an android project, and I'm not wanting to use the extended activities because I'm inserting values into the database on an AsyncTask.

在文档,它说:

如果你不希望延长 OrmLiteBaseActivity 和其他基类,那么你将需要复制它们的功能,您将需要调用 OpenHelperManager.getHelper(上下文的背景下,类openHelperClass)在你的code开始,保存助手,当你想用它为多,然后调用 OpenHelperManager。发行()当你用它做。

"If you do not want to extend the OrmLiteBaseActivity and other base classes then you will need to duplicate their functionality. You will need to call OpenHelperManager.getHelper(Context context, Class openHelperClass) at the start of your code, save the helper and use it as much as you want, and then call OpenHelperManager.release() when you are done with it."

也说要添加数据库辅助类的的strings.xml ,我有。所以我不知道我做错了。

It also says to add the database helper class in the strings.xml, which I have. So I'm not sure what I'm doing wrong.

我使用了一个名为数据访问我的数据层,看起来像这样:

I'm using a class called DataAccess for my data tier that looks like this:

public class DataAccess {
    private Context context;
    private DBHelper dbHelper;

    public DataAccess(Context _context) {
        this.context = _context;
        dbHelper = getDBHelper(_context);
    }

    private DBHelper getDBHelper(Context context) {
        if (dbHelper == null) {
            dbHelper = (DBHelper) OpenHelperManager.getHelper(context, DBHelper.class);
        }
        return dbHelper;
    }
}

和我使用的扩展的辅助类:

And I'm using the extended helper class:

public class DBHelper extends OrmLiteSqliteOpenHelper {
    private static final String DATABASE_NAME = "database.db";
    private static final int DATABASE_VERSION = 1;

    private Dao<SomeObject, Integer> someObjectTable = null;
    private ConnectionSource connectionSource = null;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
        this.connectionSource = connectionSource;
        try {
            TableUtils.createTable(connectionSource, SomeObject.class);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
    }

    public Dao<SomeObject, Integer> getSomeObjectDao() throws SQLException {
        if (someObjectTable == null) {
            dateTable = getDao(SomeObject.class);
        }
        return someObjectTable;
    }

我们的想法是创建数据访问类,并把它创建 DBHelper 若没有了。

The idea is to create the DataAccess class and have it create the DBHelper if it hasn't already.

谁能告诉我,如果这是正确的还是错误的,或者如果我在正确的道路上?

Can someone tell me if this is right or wrong, or if I'm on the right path?

谢谢!

推荐答案

您是在正确的轨道,但有点过@马特。坦白说,我从来没有做过一个项目,而不扩展我们的基类。但是,所以我创造了这个ORMLite例如项目它使用了活动和管理自己的帮手。

You are on the right track but a little off @Matt. Frankly I'd never done a project without extending our base classes. But it is a good exercise so I've created this ORMLite example project which uses an Activity and manages its own helper.

DBHelper 类是不错,但真的是你不需要你的数据访问类。在每一个你的活动(或服务......),你需要有类似如下:

Your DBHelper class is fine but really you do not need your DataAccess class. In each of your activities (or services...) you will need to have something like the following:

private DBHelper dbHelper = null;

@Override
protected void onDestroy() {
    super.onDestroy();
    if (dbHelper != null) {
        OpenHelperManager.releaseHelper();
        dbHelper = null;
    }
}

private DBHelper getHelper() {
    if (dbHelper == null) {
        dbHelper = (DBHelper)OpenHelperManager.getHelper(this, DBHelper.class);
    }
    return dbHelper;
}

您[显然],然后做一些像使用在code:

You [obviously], then use this in your code by doing something like:

Dao<SomeObject, Integer> someObjectDao = getHelper().getSomeObjectDao();

所以每当你打电话 getHelper()方法第一次,就会得到帮助通过管理,建立连接到数据库。每当你的应用程序时由操作系统被破坏,它会释放助手 - 可能关闭底层的数据库连接,如果它是最后一个发布

So whenever you call getHelper() the first time, it will get the helper through the manager, establishing the connection to the database. Whenever your application gets destroyed by the OS, it will release the helper -- possibly closing the underlying database connection if it is the last release.

请注意, OpenHelperManager.getHelper()需要上下文在情况下,第一个参数你这样做不即使是活动基类。

Notice that the OpenHelperManager.getHelper() needs the Context as the first argument in case you do this without even an Activity base class.

编辑:

如果你想创建一个数据访问键入类集中的辅助类的处理,那么你将需要做的方法静态的,做你自己的使用计数器。如果有多个活动和后台任务调用 getHelper()方法那么接下来的问题是,当你调用 releaseHelper()?你必须将每一个GET和呼叫释放计数,当计数器回来为0。但即便如此,我不是100%肯定多少行你会来救你的活动类的。

If you do want to create a DataAccess type class to centralize the handling of the helper class then you will need to make the methods static and do your own usage counter. If there are multiple activities and background tasks calling getHelper() then the question is when do you call releaseHelper()? You'll have to increment a count for each get and only call release when the counter gets back to 0. But even then, I'm not 100% sure how many lines you'd save out of your activity class.