I want to show progressdialog while doing renaming task. The dialog should appear after clicking rename button. However, the progress dialog appears after renaming function(loop) is ended. Here's my code
public void onRenameClicked(View v){
ProgressTask task = new ProgressTask();
task.execute();
for(int i = 0; i < num_of_files; i++){
rename(file[i]);
}
}
and Here's innerclass to show dialog
class ProgressTask extends AsyncTask<Integer, Void, Void>{
ProgressDialog pd = new ProgressDialog(MainActivity.this);
@Override
protected Void doInBackground(Integer... i) {
for(int j = 0; j < item.size(); j++) {
pd.setProgress(j);
pd.setMessage((j) + "/" + item.size());
}
return null;
}
@Override
protected void onPreExecute() {
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setTitle("변경중");
pd.setMax(item.size());
pd.show();
}
}
You have to use
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
pd.setProgress(values[0]);
pd.setMessage((values[0]) + "/" + values[1]);
}
and
publishProgress(j, item.size());
in the doInBackground()
. These modifications are required because the setProgress
and setMessage
must be executed in the UI thread not in the background one.
You do realize that you are calling your async task, with no data.. therfore its finishing quickly. You should probably pass in the list of files into the AsyncTask via arguments and then process those calls accordingly for each file INSIDE the AsyncTask and update the progress of the AsyncTask as you finish renaming files.
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.