7

In my app I have a ContentProvider attached to a table in a database with a CursorLoader that fills a ListView in one of my Activities. This table is filled empty by default and gets filled with user input data. I want to allow the user to completely delete all of their stored data and I'm deleting the entire database when this option is selected. The database is then recreated in it's default state when the user starts using the app again, just as it would the first time they used the app.

My issue is when I delete the database, the ContentProvider doesn't detect that the database was deleted and when I go back to my listview activity, the list is still there. I'm also making the app completely reload the ListView Activity instead of just resuming from memory and the list is still there even though the database is empty. The only way I can get the ContentProvider to reload is to kill the app in the system settings and then open it again.

Is there a way to forcefully restart the ContentProvider or to tell it that the data has been updated from outside of the ContentProvider class itself?

4

3 回答 3

3

您也可以简单地使用内容提供者的删除方法而不定义选择:

context.getContentResolver().delete(YourProvider.CONTENT_URI, null, null);
于 2015-10-13T13:15:11.467 回答
1

我在这里找到了我的问题的解决方案:https ://stackoverflow.com/a/8235584/1943155

就像在另一篇文章中一样,我在 ContentProvider 类的 onCreate 中获取了我的 dbHelper,并且在每种方法中获取了一个新的 dbHelper 解决了我的问题。

于 2013-04-22T06:17:03.623 回答
0

您想使用 cursorAdapter 填充列表视图,并在 contentProvider 中的内容发生更改时通知光标。

在 contentProvider 实现的 delete 方法中,调用contentResolver.notify 。这将通知通过调用 contentResolver 填充的游标数据发生了变化。

于 2013-04-19T21:34:32.147 回答