简体   繁体   中英

Yes no dialog with progress

I was wondering if anyone has an example of a yes no dialog that when yes is pressed it shows the progress of the background activity.

When yes is pressed my application inserts data into an SQL server database and it can take some time. Although the dialog is model while this happens it would be nice to show the user that something is working.

Cheers,

Mike.

The biggest issue with showing progress, is you've got to know how long the thing will take. I've you're going to say 25% done, then you need to know what 100% is. With a sql query, you don't really know that. You just know that it's going to take some time. So what you really want to do is not show progress, but show "busyness". You can do this with a ProgressBar set with the indeterminate property set to true.

To make an inteterminate progress bar, something like this should work:

ProgressBar progressBar = activity.findViewById(progressBarID);
progressBar.setIndeterminate(true)
progressBar.setVisibility(View.VISIBLE);
AsyncTask<Void, Void, Void> aTask = new AsyncTask<Void, Void, Void>(){
  @Override
  protected Void doInBackground(Void... arg0) {
    //Do your operations here
    return null;
  }

  @Override
  protected void onPostExecute(Void result) {
    progressBar.setVisibility(View.GONE);
            //Wrap up anything here, like closing the popup.
  }
};
aTask.execute((Void)null);

The AsyncTask is pretty cool since it will do stuff in the background off of the event queue, but call onPostExecute on the event queue. You can change the return types and parameters if you wish, I just used all voids. If you've got a custom built dialog, the progress bar can be on the dialog. If not, it can be a hidden element on the activity that you would close the yes/no dialog from and then invoke this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM