Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用sqlitecursorloader从intentservice插入sqlite_Android_Sqlite_Intentservice_Commonsware Cwac - Fatal编程技术网

Android 使用sqlitecursorloader从intentservice插入sqlite

Android 使用sqlitecursorloader从intentservice插入sqlite,android,sqlite,intentservice,commonsware-cwac,Android,Sqlite,Intentservice,Commonsware Cwac,我正在使用Commonware中的sqlitecursorloader库。我在Intentservice中收到GCM消息,并希望在sqlite数据库中插入一个条目。如果应用程序已打开,则应更新listview 我尝试在加载程序初始化的活动中使用此选项: public static void createDbEntry(String Title, String Message) { ContentValues values=new ContentValues(2); values

我正在使用Commonware中的sqlitecursorloader库。我在Intentservice中收到GCM消息,并希望在sqlite数据库中插入一个条目。如果应用程序已打开,则应更新listview

我尝试在加载程序初始化的活动中使用此选项:

public static void createDbEntry(String Title, String Message) {
    ContentValues values=new ContentValues(2);

    values.put(DatabaseHelper.TITLE, Title);
    values.put(DatabaseHelper.MESSAGE, Message);

    loader.insert("constants", DatabaseHelper.TITLE, values);
  }
在intentservice中:

MainActivity.createDbEntry(title,message);
据我所知,这在大多数情况下都是有效的,但如果加载程序被回收,我会得到一个空指针

我应该在intentservice中初始化一个新的加载程序吗


请帮助,我是android开发新手。

在静态数据成员中放置
加载程序
,对于任何
加载程序
,都是不好的,更不用说
SQLiteCursorLoader
,因为你会泄露你的
活动

我现在也正式停止了
SQLiteCursorLoader

要么:

  • 切换到使用
    ContentProvider
    和常规
    CursorLoader
    ,或

  • 使用事件总线(
    LocalBroadcastManager
    、Square的Otto、greenrobot的EventBus等)让您的服务通知您的UI层根据您的更改进行自我刷新


感谢您的回复。因此,如果我理解正确,我应该只将条目添加到sqlite db中,而不是加载程序中,然后通知ui线程在其打开时进行更新(如果不忽略)@Lorof:我会将其表述为“通知ui”,而不是“通知ui线程”。并确保无论您如何执行此操作,都不会泄漏活动或片段,例如使它们
成为静态的
。我建议的两个备选方案,如果实现正确,应该不会导致泄漏。我如何使用DatabaseHelper类直接插入数据库?@Lorof:Call
getWriteableDatabase()
,并使用
insert()
execSQL()
。游标加载程序用于加载列表视图,而不是加载数据库。您可以在asynctask中进行插入。