1

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:

  1. 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).
  2. 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.

  1. self call onUpgrade method
  2. Adding a second table in a database
  3. Upgrade SQLite database from one version to another?
  4. Creating android app Database with big amount of data
  5. 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.

Community
  • 1
  • 1
JHo
  • 1,068
  • 1
  • 14
  • 29

1 Answers1

0

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"

This is because the reload() method in the DbAdapter class is not static, but you are calling it like it is static. You have 2 options, either:

  • create an instance of type DbAdapter and call reload() on that. or
  • alternatively change the method reload to be static

Which of these is correct depends on the design of the DbAdapter class.

vdstw
  • 157
  • 6
  • Thanks vdstw. I think the design of the DbAdapter for the SampleDictionary code requires me to use your first option (i.e., declaring reload() static causes ripple affects I find difficult to solve). When you say "create an instance" and "call reload()", could you give me an example of what you mean? Thank you again. – JHo Mar 24 '11 at 09:00
  • Creating an instance is basic Java 101: `new DbAdapter()` But like I said this all depends on the design of the class. As there is no "DbAdapter" class provided I can't help you any further. – vdstw Apr 20 '11 at 08:27