Problem
I wrote a simple, Android-based "flash card" program that loads a "dictionary" from a text file (e.g., .csv, .xml) stored on the device and allows the user to step through the words and definitions.
I have been trying (unsuccessfully) to implement a menu option where the user can reload the text file (e.g., they have added words to their text file and copied it to their sdcard). The files are all relatively small ~30kB.
My code is wholesale based on the "Searchable Dictionary v2" sample app Google provides with their SDK. It uses their DbHelper.
The flash card bit works as intended but I am not able reload the text file. I think I have two issues that I am unable to solve:
- I am incorrectly trying to call the method I added to the DbHelper class to initiate "onUpgrade". I get a error in Eclipse on this (code below).
- I am incorrectly or unecessarily performing an onUpgrade() to drop the current table and read the new text file.
Here are the StackOverflow entries I have studied to solve the problem. They are helpful background but I have not been able to use them to solve my problem.
- self call onUpgrade method
- Adding a second table in a database
- Upgrade SQLite database from one version to another?
- Creating android app Database with big amount of data
- Is the onUpgrade method ever called?
Code
In the menu, there are two options. Begin program and reload dictionary file. Here is the onOptionMenuItemSelected in the simple startup activity that loads on launch:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
/*case INSERT_ID:
createNote();
return true;*/
case FLASH_CARDS:
Intent myIntent = new Intent(Cards.this, FlashCards.class);
startActivity(myIntent);
return true;
case RELOAD_FILE:
DbAdapter.reload();
}
return super.onOptionsItemSelected(item);
}
I get an error from the DbAdapter.reload() which I do not understand "Cannot make a static reference to the non-static method reload() from the type CarteDbAdapter"
And here is the reload method I added to the DB:
public void reload() {
SQLiteDatabase tableau = mDatabaseOpenHelper.getReadableDatabase();
int versionNum = tableau.getVersion();
Log.w(TAG, "Current Version: " + versionNum);
mDatabaseOpenHelper.onUpgrade(tableau, versionNum, versionNum+1);
tableau.close();
}
This gives me no errors in Eclipse.
Thank you for taking the time to read this. I hope it helps other beginners as well.