对于Android版的Listview进度对话框对话框、进度、Android、Listview

2023-09-07 02:46:46 作者:失望吧

我有一个列表视图的活动。当我把这个活动的活动时间约3-5秒出现并显示列表视图。这看起来好像按钮一直没有pssed加载活动$ P $,我想同时显示该加载一个progressdialog但不能弄明白。

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

我做了上述与列表视图,但该对话框从未展示活动的OnCreate()?

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

我想是要显示在活动A进度对话框当按钮是pressed然后解雇一旦活动B被加载并显示?

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?

感谢

推荐答案

您需要执行的AsyncTask或简单的Java线程。与AsyncTask的去现在。

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

上preExecute() - 在这里显示对话框 doInBackground() - 调用getTracks() onPostExecute() - ListView中显示曲目并关闭对话框 onPreExecute() - display dialog here doInBackground() - call getTracks() onPostExecute() - display tracks in ListView and dismiss dialog

例如:

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类来完成,只需调用执行内部的onCreate()任务的execute()方法。

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

例如:

new LoadTracksTask().execute();