1

I'm triying to do a task with an Asynctask in android.

class GetItemNames extends AsyncTask<CosmeConnection, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(CosmeConnection... cosme) {
        ContentValues cv;
        cosme[0].orderListOfNames();
        while(!(cosme[0].getState() == CosmeStates.RECEIVED_LIST_OF_NAMES)){}
        DBHandler dbHandler = new DBHandler(getApplicationContext());
        SQLiteDatabase db = dbHandler.getReadableDatabase();
        if(db != null){
            db.delete(DBHandler.TABLE_SERVER_ITEM, null, null);

            String[] nombres = cosme[0].getExistingNames().getListOfNames().toString().replaceAll("[\\[\\]]", "").split(",");
            for (String str : nombres){
                cv = new ContentValues();
                cv.put(DBHandler.SERVER_ITEM_NAME, str);
                cv.put(DBHandler.SERVER_ITEM_NAME, "N");
                try{
                    db.insert(DBHandler.TABLE_SERVER_ITEM, null, cv);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        db.close();
        dbHandler.close();
        return null;
    }

    @Override
    protected void onProgressUpdate(String... progress) {

    }

    @Override
    protected void onPostExecute(String result) {

    }
}

I'm launching this asyncktask like this:

new GetItemNames().execute(cosme, null, null);  

I want to show a dialog wiht the message "obtaining lis of names from server" while doInBackGround() is running. I tried to do in this way:

protected void onProgressUpdate(String... progress) {
        String title = getString(R.string.received_list_of_names_title);
        String msg = getString(R.string.received_list_of_names_msg);
        DialogHelper.getAlertDialog(getApplicationContext(), title, msg);
}

DialogHelper is just a class that manage dialogs, getAlertDialog() mehots is showing an normal alert dialog with a title and a message.

But this is nor working, i can't figure how to show a dialog that opens automaticlly and close when "doInBackGround()" has finished.

How can i do this?

Thanks a lot!

EDIT: after try the suggestion i have this exception:

09-21 14:07:21.966: E/AndroidRuntime(22016): FATAL EXCEPTION: main
09-21 14:07:21.966: E/AndroidRuntime(22016): Process: tfc.unizar.blasmobile, PID: 22016
09-21 14:07:21.966: E/AndroidRuntime(22016): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:643)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.app.Dialog.show(Dialog.java:286)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at tfc.unizar.blasmobile.BagContent$GetItemNames.onPreExecute(BagContent.java:263)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.os.AsyncTask.execute(AsyncTask.java:535)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at tfc.unizar.blasmobile.BagContent.onOptionsItemSelected(BagContent.java:133)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.app.Activity.onMenuItemSelected(Activity.java:2633)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1040)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:184)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.widget.AbsListView.performItemClick(AbsListView.java:1152)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.widget.AbsListView$3.run(AbsListView.java:3865)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.os.Handler.handleCallback(Handler.java:808)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.os.Handler.dispatchMessage(Handler.java:103)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.os.Looper.loop(Looper.java:193)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.app.ActivityThread.main(ActivityThread.java:5296)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at java.lang.reflect.Method.invokeNative(Native Method)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at java.lang.reflect.Method.invoke(Method.java:515)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at dalvik.system.NativeStart.main(Native Method)

The application is closed by force after this. Any suggestion?

osanfer
  • 75
  • 1
  • 7
  • possible duplicate of [Async task to show an AlertDialog](http://stackoverflow.com/questions/6494002/async-task-to-show-an-alertdialog) – Selvin Sep 21 '15 at 12:04

4 Answers4

1
private ProgressDialog mProgress;
@Override
protected void onPreExecute(){ 
   super.onPreExecute();
   mProgress = new ProgressDialog(yourContext);
   mProgress.setMessage("Obtaining list of names from server...");
   mProgress.show();    
}

@Override
protected void onPostExecute(String result){
   super.onPostExecute(result);
   mProgress.dismiss();
}
Android Developer
  • 9,157
  • 18
  • 82
  • 139
  • That's what someone else answered... as this question is a duplicate ... why are you answering again and again on the question which is a duplicate? – Selvin Sep 21 '15 at 12:02
  • Thanks for the answer, but is not working. i'm receiving a exceptión, added to the the question after editing. – osanfer Sep 21 '15 at 12:10
  • I've solved the issue, getBaseContext() wasn't a good context for dialog, change by ClassName.this and working. Thanks a lot Bhuvi! – osanfer Sep 21 '15 at 12:21
0

add this code in your Asynctask .

Initilise progressDialoge

ProgressDialog progressDialog;

and add this code is add in onPreExecute

 progressDialog = ProgressDialog.show(Form.this, "", "Loading...", true,false);

and last onPostExecute add this code

   if(progressDialog1!=null && progressDialog1.isShowing()){
            progressDialog1.dismiss();
            Log.e("DISMISS", "DISMISS");
        }
Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
0

Just do this..

private ProgressDialog mDialog;

@Override
protected void onPreExecute(){    
   mDialog = new ProgressDialog(context);
   mDialog.setMessage("loading...");
   mDialog.setIndeterminate(true);
   mDialog.setCancelable(false);
   mDialog.show();    
}

@Override
protected void onPostExecute(String result){
   mDialog.dismiss();
}
Sjd
  • 1,261
  • 1
  • 12
  • 11
0

you can't show a dialog into doInBackground because doInBackground doesn't keep in main thread (you can't get the context to show it). But methods onPreExecute and onPostExecute belong to main thread and you can call getAplicationContext() in that methods. I sguggest display dialogs in that methods.

ascripter
  • 5,665
  • 12
  • 45
  • 68
E.B
  • 11
  • 3