This Android application on Google uses the following method to refresh the database after replacing the database file with a backup:
public void resetDbConnection() {
this.cleanup();
this.db =
SQLiteDatabase.openDatabase(
"/data/data/com.totsp.bookworm/databases/bookworm.db",
null, SQLiteDatabase.OPEN_READWRITE);
}
I did not build this app, and I am not sure what happens.
I am trying to make this idea work in my own application, but the data appears to be cached by the views, and the app continues to show data from the database that was replaced, even after I call cleanup() and reopen the database. I have to terminate and restart the activity in order to see the new data.
I tried to call invalidate on my TabHost view, which pretty much contains everything. I thought that the views would redraw and refresh their underlying data, but this did also not have the expected result.
I ended up restarting the activity programmatically, which works, but this seems to be a drastic measure. Is there a better way?
Agreed with Pentium10, at least conceptually.
The application is using a
Cursorto show its data. ACursorin Android is akin to a client-side cursor in ODBC, in that it is a cached copy of all of the data represented by the query’s result set.Now, the normal way of handling changes in the database contents is to call
requery()on theCursor. That will ripple its changes through theCursorAdapterto attachedListViewsor otherAdapterViews.In your case, I am not completely certain that will work, since you are closing, replacing, and re-opening the database. That should not be done with an open
Cursoron the data, AFAIK. So, in your case, you’d need to close theCursor, do the database shuffle, then run the query again to get a freshCursoron your new database.