更新数据库android时的AlertDialog或Toast

时间:2014-01-22 16:23:12

标签: android database updating

我有一个带数据库的应用程序。在更新数据时,我希望显示AlertDialogToast转弯圈。

2 个答案:

答案 0 :(得分:2)

首先,您必须使用AsyncTask在Background Thread中执行所有事务 然后,您可以在后台线程中的事务处理过程中在ProgressDialog上简单地显示UI Thread

以下是AsyncTask的简单模板,其中包含ProgressBar

中的转弯圈a.k.a ProgressDialog
private class UpdateDataBaseAsyncTask extends AsyncTask<String, Integer, Boolean> 
{

ProgressDialog pd;  

public UpdateDataBaseAsyncTask (..Your set of Variables for updating database..)
{
    pd = new ProgressDialog(cntx);
    pd.setTitle("Updating ...");
    pd.setCancelable(false);
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);

    //Initialization of your Database Handler, and other Database Transaction related objects.

}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
    pd.show();
}

@Override
protected Boolean doInBackground(String... arg0) {
    // TODO Auto-generated method stub
    try
    {
        //Start Database Tranaction Here !
        //If the Transaction is successful, pass the boolean as true *result = true;*
    }

    catch(Exception e)
    {
        Log.e("YOUR TAG", "Error occured while updating Database !, Error = "+e.toStirng());
        //e.printStackTrace(); optional
        result = false;
    }

return result;
}

@Override
protected void onPostExecute(Boolean result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    pd.dismiss();

    if(result)
    {
        //tasks you want to perform when the database is successfully updated
    }
    else
    {
        //Show a failure Dialog here may be.
    }

}
}


修改:
Executing the AsyncTask

这是你执行它的方式:

YourActivity extends Activity
{
...
...
...
onCreate(...)
{
 //Your Implementation
 ...
 ...
 ...

 //Calling the AsyncTask
 new UpdateDataBaseAsyncTask(...).execute();
}
...
...
...
}

我希望这会有所帮助。

答案 1 :(得分:0)

我用Looper Prepare()

解决了这个问题