Android sqlite database awaiting initial dataset completion

I am following the "Search Dictionary" on Android examples and pre-populate my FTS3 database with some data (1200 records). I want to display 3 "default" records when I boot my activity, but I need to wait until all records are inserted into FTS3. When I look at magazines I see

02-17 10: 26: 44.188: D / WorldClockWidgetActivity (1188): # default cities: 0

02-17 10: 27: 11.668: D / CitiesDatabase (1188): DONE load words.

    @Override
    public void onCreate(SQLiteDatabase db) {
        mDatabase = db;
        mDatabase.execSQL(FTS_TABLE_CREATE);
        loadDictionary();
    }

    /**
     * Starts a thread to load the database table with words
     */
    private void loadDictionary() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    loadWords();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }
enter code here

      

I tried to change the above in ASyncTask, but the problem is my main activity list adapter cannot be accessed from "CitiesOpenHelper extends SQLiteOpenHelper" where data is loaded.

My question is, how can I know if my data is loaded so that I can load the defaults and show it to the user?

Many thanks

+3


source to share


1 answer


Step # 1: Move loadWords()

in onCreate()

and get rid of the thread.



Step # 2: Call getReadableDatabase()

or getWriteableDatabase()

, and your request from is AsyncTask

launched by your activity or fragment by setting ListAdapter

to onPostExecute()

.

+3


source







All Articles