我有一个listview的活动。当我调用此活动时,活动大约需要3-5秒才会显示并显示列表视图。看起来好像没有按下按钮来加载活动,我想在加载时显示progressdialog但是无法弄明白。
ProgressDialog progress;
progress = ProgressDialog.show(this, "Loading maps!",
"Please wait...", true);
// sort out track array
getTracks();
progress.dismiss();
我使用listview在活动的oncreate()上做了以上操作,但对话框从未显示过?
我想要的是在按下按钮时显示活动A的进度对话框,然后在加载并显示活动B后关闭它?
由于
答案 0 :(得分:1)
您需要实现AsyncTask或简单的JAVA线程。立即使用AsyncTask。
onPreExecute()
- 此处显示对话框doInBackground()
- 调用getTracks()onPostExecute()
- 在ListView中显示曲目并关闭对话框例如:
private static class LoadTracksTask extends AsyncTask<Void, Void, Void> {
ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = new ProgressDialog(yourActivity.this);
progress .setMessage("loading");
progress .show();
}
@Override
protected Void doInBackground(Void... params) {
// do tracks loading process here, don't update UI directly here because there is different mechanism for it
return null;
}
@Override
protected void onPostExecute(Void result) {
// write display tracks logic here
progress.dismiss(); // dismiss dialog
}
}
完成AsyncTask类的定义后,只需调用AsyncTask的onCreate()
方法,即可在execute()
内执行任务。
例如:
new LoadTracksTask().execute();
答案 1 :(得分:0)
你可以像这样进行对话:
onPreExecute(){
progressdialog = new ProgressDialog(MainActivity.this);
progressdialog.setMessage("Please wait while downloading application from the web.....");
progressdialog.setIndeterminate(false);
progressdialog.setMax(100);
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressdialog.setCancelable(false);
progressdialog.show();
}
doInBackground(String... strings){
// here you code for downloading
}
onProgressUpdate(String... progress)
{
// here set progress update
progressdialog.setProgress(Integer.parseInt(progress[0]));
}
onPostExecute(String result)
{
progressdialog.dismiss();
}
答案 2 :(得分:0)
使用类似的东西:
private static class MapLoader extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
progress.setVisibility(View.VISIBLE);
// make your element GONE
}
@Override
protected Void doInBackground(Void... params) {
// Load map processing
return null;
}
@Override
protected void onPostExecute(List<Document> result) {
progress.setVisibility(View.GONE);
adapter.setNewData(new_data);
adapter.notifyDataSetChanged();
}
}
在你的onCreate()中使用:
listView.setAdapter(adapter);
new MapLoader.execute();