0

こんにちは、スタックオーバーフローでの最初の質問です。何か間違っていたらごめんなさい。

私の問題は、アプリを初めて起動するときに、データベースに SQL データを書き込む必要があることです。SQLの書き込み中にonCreate()でそれを行うと、ビューが表示されないため、フリーズするようになります。そして、そこにローダーを作成すると、最初にすべての oncreate を終了し、その後ビューを表示する必要があるため、表示されません。次に、SQLが空の場合にAlertDialogを実行して、「インストール」が必要であることをユーザーに通知し、確認後、実行中にSQLを挿入してロードボックスを作成したかった...

だから私の質問は次のとおりです:onCreateをロードする方法...アクティビティの実行時に...良いアイデアを教えてください...ここに私が作成したコードがあります:

private DatabaseHandler db = new DatabaseHandler(this);

Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    context = this;     

    if(db.getWordsCount() == 0)
    {           
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Instalation needed");
        builder.setMessage("Database need to be installed");
        builder.setPositiveButton("Ok", dialogClickListener);
        builder.setNegativeButton("Close", dialogClickListener);
        builder.show();
    }
}

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
        switch (which){ 
        case DialogInterface.BUTTON_POSITIVE:

                    ProgressDialog progress = new ProgressDialog(context);
              progress.setTitle("Instalation");
              progress.setMessage("Please wait...");
                progress.show();                
                db.insertWords(db);
                progress.cancel();
            break; 

        case DialogInterface.BUTTON_NEGATIVE: 
            finish();
            break; 
        } 
    } 
}; 
4

1 に答える 1

0

同じ例がたくさんあります:

http://ashwinrayaprolu.wordpress.com/2011/03/15/android-database-example-database-usage-asynctask-database-export/

private class InsertDataTask extends AsyncTask {
        private final ProgressDialog dialog = new ProgressDialog(
                DatabaseActivity.this);

        // can use UI thread here
        protected void onPreExecute() {
            this.dialog.setMessage("Inserting data...");
            this.dialog.show();
        }

        // automatically done on worker thread (separate from UI thread)
        protected Void doInBackground(final String... args) {
            DatabaseActivity.this.application.getDataHelper().insert(args[0]);
            return null;
        }

        // can use UI thread here
        protected void onPostExecute(final Void unused) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }
            // reset the output view by retrieving the new data
            // (note, this is a naive example, in the real world it might make sense
            // to have a cache of the data and just append to what is already there, or such
            // in order to cut down on expensive database operations)
            new SelectDataTask().execute();
        }
    }
于 2012-05-26T18:45:49.250 に答える