【问题标题】:Using OrmLite in AppWidget RemoteViewFactory在 AppWidget RemoteViewFactory 中使用 OrmLite
【发布时间】:2015-03-13 21:13:10
【问题描述】:

我需要从RemoteViewFactory 类的数据库中加载数据,我正在使用 OrmLite。 我有这个Helper 类:

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "gfkksa.db";
    private static final int DATABASE_VERSION = 1;


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

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, Issue.class);
            TableUtils.createTable(connectionSource, IssuePriority.class);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Can't create database.");
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
            int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource, Issue.class, true);
            TableUtils.dropTable(connectionSource, IssuePriority.class, true);
            onCreate(db, connectionSource);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Can't drop databases.");
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    public HashMap<String, Dao> getDaoFactory() {

        HashMap<String, Dao> hashFactory = new HashMap<String, Dao>();
        try {
            hashFactory.put("issue", getDao(Issue.class));
            hashFactory.put("issuePriority", getDao(IssuePriority.class));
        } catch (java.sql.SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return hashFactory;
    }
}

一切正常,在 Activity 类 (extends OrmLiteBaseActivity&lt;DatabaseHelper&gt;) 中,例如我正在从 DB 中获取数据列表,如下所示:

ArrayList items = (ArrayList) getHelper().getDaoFactory().get("issue").queryForAll();

但如果我在实现RemoteViewsService.RemoteViewsFactory(AppWidget 的ListView 的远程视图工厂)的课堂上做同样的事情,应用程序会崩溃并显示以下异常消息:

03-18 16:13:29.244: E/AndroidRuntime(28500): java.lang.RuntimeException: Unable to bind to service cz.testbrana.widget.WidgetService@41e34110 with Intent { dat=intent: cmp=cz.testbrana.ebranasystem/cz.testbrana.widget.WidgetService (has extras) }: java.lang.IllegalStateException: A call has not been made to onCreate() yet so the helper is null

如何在 AppWidget 的 RemoteViewFactory 中使用 OrmLite?

【问题讨论】:

    标签: android sqlite ormlite android-appwidget remoteview


    【解决方案1】:

    我找到了答案,当使用来自官方文档 (http://ormlite.com/docs/android) 的方法 getHelper()onDestroy() 而不是扩展 DBHelper 时,一切都很好。

    private DatabaseHelper databaseHelper = null;
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (databaseHelper != null) {
            OpenHelperManager.releaseHelper();
            databaseHelper = null;
        }
    }
    
    private DBHelper getHelper() {
        if (databaseHelper == null) {
            databaseHelper =
                OpenHelperManager.getHelper(this, DatabaseHelper.class);
        }
        return databaseHelper;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多