Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么sqlite数据库的异步任务处理程序会立即触发postExecute_Android_Asynchronous_Android Asynctask_Android Sqlite_Android Progressbar - Fatal编程技术网

Android 为什么sqlite数据库的异步任务处理程序会立即触发postExecute

Android 为什么sqlite数据库的异步任务处理程序会立即触发postExecute,android,asynchronous,android-asynctask,android-sqlite,android-progressbar,Android,Asynchronous,Android Asynctask,Android Sqlite,Android Progressbar,我有一个async类,它打开了一个sqlite数据库,如果是第一次运行,数据库将填充大约需要10秒才能完成的数据。即使仍有工作在后台完成,异步任务也会立即完成。我认为这与sqlite的oncreate方法有关,可能是在执行另一个异步任务之类的事情 class BuildDatabase extends AsyncTask<Void, Void, Void> { ProgressDialog loading; @Override protected void

我有一个async类,它打开了一个sqlite数据库,如果是第一次运行,数据库将填充大约需要10秒才能完成的数据。即使仍有工作在后台完成,异步任务也会立即完成。我认为这与sqlite的oncreate方法有关,可能是在执行另一个异步任务之类的事情

class BuildDatabase extends AsyncTask<Void, Void, Void> {

    ProgressDialog loading;

    @Override
    protected void onPreExecute() {
        loading = ProgressDialog.show(ctx, "Please wait...", "Building database, this may take a few seconds", true);
    }

    @Override
    protected Void doInBackground(Void... params) {
        // open up the database, if its the first time running the data will
        // be populated
        db = new SQLiteWrapper(ctx); //takes 1-10 seconds
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        Log.d(TAG,"post execute");
        loading.dismiss();
    }
}
如果您熟悉此类,则只调用onCreate,而数据库不存在。 如果在数据库构造函数中手动调用onCreate(),将显示进度对话框。
想法?

onCreate
只有在您实际打开数据库时才会被调用(而且数据库还不存在)

this.getWritebleDatabase()
添加到
SQLiteWrapper
构造函数中,如下所示:

public SQLiteWrapper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    ctx = context;
    this.getWritableDatabase();
}
public SQLiteWrapper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    ctx = context;
    this.getWritableDatabase();
}