开发者

Resetting or refreshing a database connection

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 appea开发者_运维百科rs 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 Cursor to show its data. A Cursor in 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 the Cursor. That will ripple its changes through the CursorAdapter to attached ListViews or other AdapterViews.

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 Cursor on the data, AFAIK. So, in your case, you'd need to close the Cursor, do the database shuffle, then run the query again to get a fresh Cursor on your new database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜