进度对话框显示到最晚(异步任务)
由于异步任务,我动态更新了我的 UI。 在 onpreexecute 方法中,我显示了进度对话框,但它显示得很晚。
我的意思是,我单击一个按钮来更改活动,然后 UI 被冻结,之后我简要地看到进度对话框,最后我的 UI 更新了。
请问我的代码有什么问题吗?
private ProgressDialog pd ;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
new taskDoSomething().execute();
}
private class taskDoSomething extends AsyncTask<Long, Integer, Integer>
{
protected void onPreExecute() {
// TODO Auto-generated method stub
pd = ProgressDialog.show(SQLView.this, "Working..", "Calculating", true,true);
}
protected Integer doInBackground(Long... params) {
UpdateIHM();
return 0;
}
protected void onPostExecute(Integer result) {
Toast.makeText(SQLView.this,"onPostExecute", Toast.LENGTH_LONG).show();
}
}
public void UpdateIHM()
{
runOnUiThread(new Runnable() {
@Override
public void run() {
LinearLayout my_layout = (LinearLayout) findViewById(R.id.lil_content);
HotorNot info = new HotorNot(SQLView.this);
my_table data = new my_table();
Log.i("Test","ok 0");
info.open();
Log.i("Test","ok 0.1");
for(int i =0; i<3; i++){
data = info.getData();
for (int j=0; j<50; j++){
LinearLayout lil_temp = new LinearLayout(SQLView.this);
lil_temp.setOrientation(LinearLayout.HORIZONTAL);
lil_temp.addView(buildText(data.row), getParams(WRAP, WRAP));
lil_temp.addView(buildText(data.name), getParams(WRAP, WRAP));
lil_temp.addView(buildText(data.hotness), getParams(WRAP, WRAP));
my_layout.addView(lil_temp);
}
}
info.close();
handler.sendEmptyMessage(102);
}
});
}
I update dynamically my UI thanks to an asynctask.
On the onpreexecute method I show the progressdialog, but it shows up to late.
I mean, I click on a button to change of activity, then the UI is frozen, and ater I see briefly the progressdialog and finally my UI updated.
What is wrong in my code please ?
private ProgressDialog pd ;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
new taskDoSomething().execute();
}
private class taskDoSomething extends AsyncTask<Long, Integer, Integer>
{
protected void onPreExecute() {
// TODO Auto-generated method stub
pd = ProgressDialog.show(SQLView.this, "Working..", "Calculating", true,true);
}
protected Integer doInBackground(Long... params) {
UpdateIHM();
return 0;
}
protected void onPostExecute(Integer result) {
Toast.makeText(SQLView.this,"onPostExecute", Toast.LENGTH_LONG).show();
}
}
public void UpdateIHM()
{
runOnUiThread(new Runnable() {
@Override
public void run() {
LinearLayout my_layout = (LinearLayout) findViewById(R.id.lil_content);
HotorNot info = new HotorNot(SQLView.this);
my_table data = new my_table();
Log.i("Test","ok 0");
info.open();
Log.i("Test","ok 0.1");
for(int i =0; i<3; i++){
data = info.getData();
for (int j=0; j<50; j++){
LinearLayout lil_temp = new LinearLayout(SQLView.this);
lil_temp.setOrientation(LinearLayout.HORIZONTAL);
lil_temp.addView(buildText(data.row), getParams(WRAP, WRAP));
lil_temp.addView(buildText(data.name), getParams(WRAP, WRAP));
lil_temp.addView(buildText(data.hotness), getParams(WRAP, WRAP));
my_layout.addView(lil_temp);
}
}
info.close();
handler.sendEmptyMessage(102);
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在执行
AsyncTask
之前显示您的ProgressDialog
。这通常会有帮助。Try showing your
ProgressDialog
just before executing theAsyncTask
. That'll usually help.我正在做同样的事情,只是我在 onPostExecute() 的开头调用 ProgressDialog.dismiss() 。
I am doing the same thing except that I call ProgressDialog.dismiss() at the beginning of onPostExecute().