How to open sqlite DB in onrestart()
-
26-10-2019 - |
Question
I am updating home listview using data retrieved from DB. When I go forth then comeback to Home it crashes
My code is here
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart called");
DatabaseManager.getInstance(HomeActvity.this).open();
listAdapter.notifyDataSetChanged();
DatabaseManager.getInstance(HomeActvity.this).closeDatabase();
}
My Log is:
11-28 19:07:22.606: E/AndroidRuntime(24303): java.lang.IllegalStateException: database not open
11-28 19:07:22.606: E/AndroidRuntime(24303): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1224)
11-28 19:07:22.606: E/AndroidRuntime(24303): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
11-28 19:07:22.606: E/AndroidRuntime(24303): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
I tried runOnUIThread()..
Solution
Why do you close your database right after notifydatachanged? I think database reading is happening after close. Try to remove the line and see if it's working.
DatabaseManager.getInstance(HomeActvity.this).open();
listAdapter.notifyDataSetChanged();
DatabaseManager.getInstance(HomeActvity.this).closeDatabase();
OTHER TIPS
You are doing nothing in between open and close! how come..
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow