Listview的Android进度对话框

我有一个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后关闭它?

谢谢

您需要实现AsyncTask或简单的JAVA线程。 立即使用AsyncTask。

  • onPreExecute() – 在这里显示对话框
  • doInBackground() – 调用getTracks()
  • onPostExecute() – 在ListView和dismiss对话框中显示轨道

例如:

 private static class LoadTracksTask extends AsyncTask { 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的execute()方法execute() onCreate()内的任务。

例如:

 new LoadTracksTask().execute(); 

您可以像这样进行对话:

 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(); } 

使用这样的东西:

 private static class MapLoader extends AsyncTask { @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 result) { progress.setVisibility(View.GONE); adapter.setNewData(new_data); adapter.notifyDataSetChanged(); } } 

在你的onCreate()中使用:

 listView.setAdapter(adapter); new MapLoader.execute();