[英]I'm making a renaming application in Android
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()
. 在
doInBackground()
。 These modifications are required because the setProgress
and setMessage
must be executed in the UI thread not in the background one. 这些修改是必需的,因为
setProgress
和setMessage
必须在UI线程中而不是在后台线程中执行。
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.
你或许应该在传递通过参数文件到的AsyncTask的列表,然后相应地处理这些电话为每个文件里面的AsyncTask和更新的AsyncTask的进度完成重命名文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.