Domanda

I'm following the 'Searchable Dictionary' of Android examples and pre-populating my FTS3 database with some data (1200 records). I want to display 3 'default' records on initial load of my activity but I need to wait until all the records have been inserted into FTS3. When I look at the logs I see

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

02-17 10:27:11.668: D/CitiesDatabase(1188): DONE loading 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 to an ASyncTask but the issue is that my main activity's list adapter cannot be accessed from "CitiesOpenHelper extends SQLiteOpenHelper" where the data is being loaded.

My question was how can I tell if my data has been loaded so I can load default values and display to the user?

Thanks very much

È stato utile?

Soluzione

Step #1: Move loadWords() into onCreate() and get rid of the thread.

Step #2: Call getReadableDatabase() or getWriteableDatabase() and your query from an AsyncTask started by your activity or fragment, setting your ListAdapter in onPostExecute().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top