Friday, August 7, 2009

Android: displaying one dialog after another

So having got my progress bar dialog to appear I now find myself prevented from displaying a second dialog to announce the results of the long running action.

Here is the code:

   final ProgressDialog myOtherProgressDialog = new ProgressDialog(
     this);
   myOtherProgressDialog.setTitle("Please Wait ...");
   myOtherProgressDialog.setMessage("Adding item to study list ...");
   myOtherProgressDialog.setIndeterminate(true);
   myOtherProgressDialog.setCancelable(true);

   // TODO spinner not showing for some reason ...
   final AlertDialog dialog = new AlertDialog.Builder(this).create();

   final Thread add = new Thread() {
    public void run() {
     AddItemResult add_item_result = addItemToList(
       Main.default_study_list_id,
       (String) item.item_node.atts.get("id"));
     
     dialog.setTitle(add_item_result.getTitle());
     dialog.setMessage(add_item_result.getMessage());
     dialog.setButton("OK",
       new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
          int which) {
        
         return;
        }
       });
     
     myOtherProgressDialog.dismiss();
     //Looper.prepare();
     dialog.show();
    }
   };
   myOtherProgressDialog.setButton("Cancel",
     new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
       add.interrupt();
      }
     });
   OnCancelListener ocl = new OnCancelListener() {
    public void onCancel(DialogInterface arg0) {
     add.interrupt();
    }
   };
   myOtherProgressDialog.setOnCancelListener(ocl);
   closeMenu();
   myOtherProgressDialog.show();
   add.start();

The problem is that if I just try to show the second dialog I get this error:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

and if I try and edit the existing progress dialog to show the results of the long running action I am told:

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Which is a bit frustrating. Is my only option to display results in a completely new activity?

1 comment:

Sam Joseph said...

found solution was to use this:

public void onWindowFocusChanged(boolean bool)
{
super.onWindowFocusChanged(bool);
Log.d("DEBUG", "onWindowFocusChanged");
if (ItemActivity.add_item_result != null){
final AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setTitle(ItemActivity.add_item_result.getTitle());
dialog.setMessage(ItemActivity.add_item_result.getMessage());
dialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
/* myProgressDialog = ProgressDialog
.show(
ItemActivity.this,
"Please wait...",
"Downloading SmartFm list items ...",
true, true);
// need to go back to refreshed set of items
// for that list
reload.start();*/
return;
}
});
ItemActivity.add_item_result = null;
dialog.show();
}
}