开发者

Progressbar spinner is not rotating

Can anyone tell me why progress bar wheel is not rotating ... I am trying to put a progress bar while downloading data from web server everything is fine till now...i am able to set the progress bar while downloading data but the problem progress bar spinner is not rotating....Below is my code for progress bar:

<Progressbar 
    android:id="@+id/xPBar"
    androi开发者_如何学Cd:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    style="@android:style/Widget.ProgressBar.Inverse"/>

this is the downloading code: while executing this method i am showing the progress bar

    public void downloadAlerts() {
    mPBar.setVisibility(ProgressBar.VISIBLE);
    Runnable r = new Runnable() {

        @Override
        public void run() {
            if (checkNetworkStatus(getApplicationContext()) == true) {
                String alert = con.execute(ALERTS_URL
                        );
                AlertsParser parser = new AlertsParser();
                parser.parseJson(alert);
                startActivity(new Intent(getApplicationContext(), Alerts.class));
                onSuccessDownload();
            } else if (checkNetworkStatus(getApplicationContext()) == false) {
                onFailureDownload();
            }

        }
    };
    mHandler.post(r);

}

Thanks...


You likely have to put it into an AsyncTask. If you try to put the progress bar in your main UI thread then it won't move if you do something else in the background as explained here.


By calling

mHandler.post(r)

you are not running the runnable in a new Thread, but instead you are posting it in the message queue - this means in the main UI thread. This causes the app to freeze while your data is downloading.

You should run either AsyncTask or new thread. For example put at the end of your Runnable r something like

Runnable r = new Runnable() {

   @Override
    public void run() {
        .......
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                hideProgressOnSuccess_andShowData();
            }
        });
    }

}

and then run your Runnable r like this:

new Thread(r).start();

With AsyncTask and using its onPreExecute and onPostExecute it can be more elegant, of course.

Greets, Dan

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜