Android progress dialog for Listview

别等时光非礼了梦想. 提交于 2020-02-02 16:31:33

问题


I have an activity with a listview. When I call this activity the activity takes about 3-5 seconds to appear and display the listview. It looks as if the button has not been pressed to load the activity, i would like to display a progressdialog while this loads but can't figure it out.

ProgressDialog progress;
    progress = ProgressDialog.show(this, "Loading maps!",
        "Please wait...", true);


    // sort out track array
    getTracks();

            progress.dismiss();

I did the above on the oncreate() of the activity with the listview but the dialog never shows?

What I would like is to show the progress dialog on Activity A when the button is pressed and then dismiss once Activity B is loaded and displayed?

Thanks


回答1:


You need to implement AsyncTask or simple JAVA threading. Go with AsyncTask right now.

  • onPreExecute() - display dialog here
  • doInBackground() - call getTracks()
  • onPostExecute() - display tracks in ListView and dismiss dialog

For example:

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
    }
}

Once you are done with defining your AsyncTask class, just execute the task inside onCreate() by calling execute() method of your AsyncTask.

For example:

new LoadTracksTask().execute();



回答2:


You can make progress Dialog like this :

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



回答3:


Use something like this:

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

In your onCreate() use:

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


来源:https://stackoverflow.com/questions/19926157/android-progress-dialog-for-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!