I have an app with a database. While the data are updating, I'd like to show an AlertDialog
or a Toast
with a turning circle.
First of all, you would have to do all the transactions in Background Thread
using AsyncTask.
You can then simply display a ProgressDialog
on the UI Thread
while the transaction processes in the Background Thread.
Here is a simple Template of AsyncTask
that includes a turning circle a.k.a ProgressBar
within the 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.
}
}
}
Edit:
Executing the AsyncTask
This is how you can execute it:
YourActivity extends Activity
{
...
...
...
onCreate(...)
{
//Your Implementation
...
...
...
//Calling the AsyncTask
new UpdateDataBaseAsyncTask(...).execute();
}
...
...
...
}
I hope this helps.