0

嗨,如果我做错了什么,这是我在 stackoverflow 中的第一个问题。

我的问题是,当我第一次启动应用程序时,我需要在数据库中写入 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 回答