【问题标题】:Making Loader object as singleton in Android在 Android 中将 Loader 对象设置为单例
【发布时间】:2014-06-08 07:25:54
【问题描述】:

我正在使用 commonsware 的 Sqlite 加载器库,即cwac-loaderex。将 Sqlite 加载器对象设为单例是一种好习惯吗?

比方说,在一个新活动中,我想插入一条新记录,所以我必须对 Loader 对象执行此操作以使其反映在我的 UI 上。

但在新活动中,我不想像这样加载表格的所有内容

loader=
        new SQLiteCursorLoader(this, db, "SELECT _ID, title, value "
            + "FROM constants ORDER BY title", null);

相反,通过单例使用共享加载器对象好不好?

编辑:

yes i know its deprecated. Here my question is. Lets say that i have created a sqlite loader object and displayed the contents on listview, and i will click on add new item button, it will take me to a new activity, ill fill out the details and press add. In this case the added object to reflect on listview we need to perform insert operation on sqliteloader object, so should i have to pass this object from listview via intent or is it ok to create a new sqliteloader object in the current activity and perform operation?

【问题讨论】:

    标签: android sqlite android-loadermanager commonsware-cwac


    【解决方案1】:

    我正在使用 commonsware 的 Sqlite 加载器库,即 cwac-loaderex。

    请不要。该项目已停止,正如项目主页上的全部大写粗体所提到的那样。

    将 Sqlite 加载器对象设为单例是一种好习惯吗?

    没有。

    【讨论】:

    • 嗨,我已经用粗体编辑了我的答案,你能告诉我我代表的方式是否正确。我知道它已被弃用,但我仍然需要它,因为我正在我的项目中。谢谢
    • @Naruto:“所以我应该通过意图从列表视图传递这个对象,还是可以在当前活动中创建一个新的 sqliteloader 对象并执行操作?” - 两者都不。您应该有一个单独的 SQLiteDatabase 实例(NOT SQLiteCursorLoader),并且您的第二个活动应该使用 SQLiteDatabase 对象来修改数据库。您需要重写SQLiteCursorLoader,以便您可以手动告诉它在您返回第一个活动时重新加载Cursor
    • 我是这样做的,public void refreshTheTaskList() { if(mTLoader == null) { getLoaderManager().restartLoader(mSelectedListID,null,this); } else { mTLoader.onContentChanged(); } }。有效。非常感谢您的帮助。非常感谢
    猜你喜欢
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多