【发布时间】:2012-06-05 15:52:32
【问题描述】:
我有一个想要执行以下操作的应用:
- 使用按钮和 TextView 显示活动。
- 用户点击按钮,应用显示进度对话框。
- 应用调用网络服务来获取列表。
- 进度对话框被隐藏,并出现一个列表选择对话框以显示检索到的列表。
- 用户选择列表中的一项。
- 项目显示在 TextView 中。
问题是这种情况发生了:
- 使用按钮和 TextView 显示活动。
- 用户点击按钮,按钮状态变为选中状态。
- 几秒钟后,列表选择对话框出现,显示检索到的列表。
- 用户选择列表中的一项。
- 进度对话框显示几秒钟,然后隐藏。
- 项目显示在 TextView 中。
Web 服务在 AsyncTask 中执行,进度对话框显示在 onPreExecute() 方法中,并在 onPostExecute() 方法中关闭:
public class WebService extends AsyncTask<Void, Void, Boolean> {
public void onPreExecute() {
_progressDialog = ProgressDialog.show(_context, "", message);
}
protected Boolean doInBackground(Void... params) {
try {
// Execute web service
}
catch (Exception e) {
e.printStackTrace();
}
return true;
}
protected void onPostExecute(Boolean result) {
_progressDialog.hide();
}
}
执行网络服务和显示对话框的代码:
WebService ws= new WebService();
ws.execute();
// Web service saves retrieved list in local db
// Retrieve list from local db
AlertDialog.Builder db = new AlertDialog.Builder(context);
db.setTitle("Select an Item");
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(context,
android.R.layout.simple_selectable_list_item, list);
db.setAdapter(listAdapter, null);
db.show();
我是否需要在代码中添加一些内容以确保进度对话框显示在列表选择对话框之前?
提前谢谢你。
【问题讨论】:
标签: android android-asynctask progressdialog