我有一个带数据库的应用程序。当数据更新时,我想显示一个带旋转圆的AlertDialogToast

最佳答案

首先,您必须使用asynctask完成Background Thread中的所有事务。
然后,当事务处理在后台线程中时,您只需在ProgressDialog上显示UI Thread
这里有一个简单的AsyncTask模板,其中包括ProgressBar中的一个回转圆a.k.aProgressDialog

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();
}
...
...
...
}

我希望这能有帮助。

关于android - 更新数据库android时使用AlertDialog或Toast ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21288381/

10-11 22:37
查看更多