0

I have a sqlite db with a lot of data, I'm trying to display a ProgressBar while deleting the db tables. How do I post my method deleteAll in the class AsyncTask?

    public void deleteAll(){
    AlertDialog.Builder builder2=new AlertDialog.Builder(Bi.this);
    builder2.setTitle(getString(R.string.titolo_alert_elimina_tutto));
    builder2.setMessage(getString(R.string.testo_alert_elimina_tutto));
    builder2.setPositiveButton("OK",new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {


    SQLiteDatabase db= mHelper.getWritableDatabase();
    db.delete(iTable.TABLE_NAME, null, null);
    db.delete(mTable.TABLE_NAME, null, null);
          .....

The class AsyncTask

protected class LoadDataTask extends AsyncTask<Context, Integer, String>
{
ProgressDialog myLoadingDialog;

@Override
protected void onPreExecute()
{
myLoadingDialog = new ProgressDialog(Bilancio.this);
myLoadingDialog.setMessage("Loading");
    myLoadingDialog.setIndeterminate(false);
    myLoadingDialog.setCancelable(false);
    myLoadingDialog.show();
    super.onPreExecute();
}

@Override
protected String doInBackground(Context... arg0)
{

    deleteAll();
    return null;
}

@Override
protected void onPostExecute(String result)
{
    //showData();
    myLoadingDialog.dismiss();
    super.onPostExecute(result);
}
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • All dialogs need to be displayed in onPreExecute() or in onPostExecute() avoid dialog popup in doItInBackground() – Seraphim's Nov 15 '13 at 17:22
  • Are you deleting all data from all the tables in the DB? If so, just delete the file. – 323go Nov 15 '13 at 17:25

2 Answers2

0

Call AsyncTask on your Dialog button's click and then do all your deletion in the doInBackground method

Apoorv
  • 13,470
  • 4
  • 27
  • 33
  • Sorry, as I call **AsyncTask** in the button "OK" in **AlertDialog**? – user2996988 Nov 15 '13 at 17:28
  • Yes i am saying the same thing just shift the deletion code to the `doInBackground` method of your `AsyncTask` instead of `onClick` of your `Dialog` button – Apoorv Nov 15 '13 at 17:31
0

I suggest to move the dialog management in the activity class, and execute the AsyncTask from there:

AlertDialog.Builder builder2 = new AlertDialog.Builder(Bi.this);
builder2.setTitle(getString(R.string.titolo_alert_elimina_tutto));
builder2.setMessage(getString(R.string.testo_alert_elimina_tutto));
builder2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
        new LoadDataTask().execute(getContext());
   }
});

And the deleteAll():

public void deleteAll() {
    SQLiteDatabase db= mHelper.getWritableDatabase();
    db.delete(iTable.TABLE_NAME, null, null);
    db.delete(mTable.TABLE_NAME, null, null);
    ...
}

Anyway, you don't neet to pass the context to the AsyncTask, because you define the LoadDataTask inside (as subclass of) the activity class so it become:

protected class LoadDataTask extends AsyncTask<Void, Integer, String>

inside the AsyncTask class you can access getContext() from everywhere!

Seraphim's
  • 12,559
  • 20
  • 88
  • 129